I use OHLC re-sampling of 1min time series data in Pandas, the 15min will work perfectly, for example on the following dataframe:
ohlc_dict = {'Open':'first', 'High':'max', 'Low':'min', 'Close': 'last'}
df.resample('15Min').apply(ohlc_dict).dropna(how='any').loc['2011-02-01']
Date Time Open High Low Close
------------------------------------------------------------------
2011-02-01 09:30:00 3081.940 3086.860 3077.832 3081.214
2011-02-01 09:45:00 3082.422 3083.730 3071.922 3073.801
2011-02-01 10:00:00 3073.303 3078.345 3069.130 3078.345
2011-02-01 10:15:00 3078.563 3078.563 3071.522 3072.279
2011-02-01 10:30:00 3071.873 3071.873 3063.497 3067.364
2011-02-01 10:45:00 3066.735 3070.523 3063.402 3069.974
2011-02-01 11:00:00 3069.561 3069.981 3066.286 3069.981
2011-02-01 11:15:00 3070.602 3074.088 3070.373 3073.919
2011-02-01 13:00:00 3074.778 3074.823 3069.925 3069.925
2011-02-01 13:15:00 3070.096 3070.903 3063.457 3063.457
2011-02-01 13:30:00 3063.929 3067.358 3063.929 3067.358
2011-02-01 13:45:00 3067.570 3072.455 3067.570 3072.247
2011-02-01 14:00:00 3072.927 3081.357 3072.767 3080.175
2011-02-01 14:15:00 3078.843 3079.435 3076.733 3076.782
2011-02-01 14:30:00 3076.721 3081.980 3076.721 3081.912
2011-02-01 14:45:00 3082.822 3083.381 3076.722 3077.283
However, when I resample 1min to 1H, the problem comes out. I use default setting, and find the time start from 9 am, but the markert open at 9:30 am.
df.resample('1H').apply(ohlc_dict).dropna(how='any').loc['2011-02-01']
I then try to change the base
setting, but fail in the afternoon session. The market should open at 13 pm and end at 15 pm, so there should be 13 pm, 14 pm, 15 pm, total 3 bars.
df.resample('60MIN',base=30).apply(ohlc_dict).dropna(how='any').loc['2011-02-01']
In conclusion, the problem is I want it fitting in market and has 6 bars (9:30,10:30,11:30,1:00,2:00,3:00)
, but resample
in pandas
only give me 5 bars (9:30,10:30,11:30,1:30,2:30)
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Thanks.
resample
is in fixed interval. You can try to resample into 2 separate dfs, then slice the dfs and join them together. – Yeile