DataFrame resample ohlc from daily to weekly

joel
Jul 12, 2020
import pandas_datareader as pdr# Load the Data
datadf = pdr.DataReader(“MSFT”,
start=’2019–1–1',
end=’2020–12–31',
data_source=’yahoo’)[[‘Open’, ‘High’, ‘Low’, ‘Close’, ‘Volume’]]
data = bt.feeds.PandasData(dataname=datadf)
cerebro.adddata(data) # First add the original data — smaller timeframe
# this is key function to resample data to weekly
datadf1 = datadf.resample(‘W’, label=’left’).agg({
‘Open’: ‘first’,
‘High’: ‘max’,
‘Low’: ‘min’,
‘Close’: ‘last’,
‘Volume’: ‘sum’
})
datadf1.reset_index(level=0, inplace=True)
# have to add 1 day to each ‘Date’, tried many ways, only this works.
datadf1[‘Date’] = pd.to_datetime(datadf1[‘Date’]).apply(pd.DateOffset(1))
datadf1 = datadf1.set_index(‘Date’)
# end of resample to weekly

--

--