I'm just getting started playing around with Python and Pandas, with ~10 hours total invested to far. I have a dataframe of daily stock data and I've resampled it weekly. The problem lies in weeks where Friday is a holiday, I get NaN in my dataset. Is there a way to accommodate for this scenario? (Same issue as well when I resample monthly, where the final day is a weekend).
sample = 'W-FRI'
for i in range(tickerCount):
datalist.append(yf.download(stock_list[i], start, end))
datalist[i]['High'] = datalist[i]['High'].resample(sample).max()
datalist[i]['Low'] = datalist[i]['Low'].resample(sample).min()
datalist[i]['Open'] = datalist[i]['Open'].resample(sample).first()
datalist[i]['Close'] = datalist[i]['Close'].resample(sample).last()
datalist[i] = datalist[i].asfreq(sample, method='pad')
As you can see the week of Good Friday could not be sampled properly. I know its possible to remove these from the dataframe:
datalist[i] = datalist[i][datalist[i]['High'].notna()]
But ideally I would like to grab the last day of data for the specified resampled period (In this case, use Thursday's data. I've looked at this answer
Is there a way to accomplish this?
EDIT:
@ElliottCollins had an idea to use .ffill() to backfill the Friday with the previous data (from Thursday). This also backfills every Saturday and Sunday with the previous data. Unfortunately when I do this and then resample W-FRI my Open values are incorrect; They become Previous Friday's open rather than Monday's Open
EDIT 2
I just realized if I set index again after all this, I'm able to resample as desired. I'll post the solution below