I have 1 minute timeframe olhc candle data of intraday stock (in pandas dataframe) . I want to convert 1 minute olhc intraday stock data (present in pandas dataframe) into higher timeframe olhc data like 5min,10min,15min olhc data .
open low high close volume Adj_Close
2020-07-24 13:12:00 191.00 190.95 191.00 190.95 21131 190.95
2020-07-24 13:11:00 190.80 190.80 190.95 190.85 88030 190.85
2020-07-24 13:10:00 191.25 190.80 191.25 190.80 163046 190.80
2020-07-24 13:09:00 191.15 191.15 191.20 191.20 71910 191.20
2020-07-24 13:08:00 191.10 191.05 191.20 191.10 100514 191.10
... ... ... ... ... ... ...
2020-07-23 12:51:00 194.90 194.75 194.90 194.75 44430 194.75
2020-07-23 12:50:00 194.85 194.75 194.85 194.85 116263 194.85
2020-07-23 12:49:00 194.85 194.85 194.95 194.95 34569 194.95
2020-07-23 12:48:00 194.70 194.70 194.85 194.85 154293 194.85
2020-07-23 12:47:00 194.95 194.80 194.95 194.80 145786 194.80
The agg()
function lets you pass in a dictionary of functions, for column-specific aggregations. We will create two dictionaries: one for aggregation logic, and the other to rename columns:
def resample_stock_data(df, timedelta):
# make a copy
df = df.copy()
# convert index to datetime
df.index = pd.to_datetime(df.index)
# sort the index (evidently required by resample())
df = df.sort_index()
aggregation_dict = {
'volume': 'mean',
'open': 'sum',
'high': 'sum',
'low': 'sum',
'close': 'sum',
'Adj_Close': 'sum'
}
rename_dict = {
'open': 'first',
'high': 'max_price',
'low': 'min_price',
'close': 'last_price',
'volume': 'vol (shares)',
'Adj_Close': 'last',
}
return (df
.resample(timedelta)
.agg(aggregation_dict)
.rename(columns=rename_dict)
)