data=
open high low close volume
timestamp
2015-02-02 09:15:00+05:30 1280.00 1286.00 1280.00 1284.55 5847
2015-02-02 09:16:00+05:30 1284.55 1290.00 1283.90 1284.05 260
2015-02-02 09:17:00+05:30 1284.05 1288.20 1280.30 1283.90 424
2015-02-02 09:18:00+05:30 1283.90 1285.75 1283.90 1285.35 40
2015-02-02 09:19:00+05:30 1285.35 1290.00 1283.30 1285.40 203
2015-02-02 09:20:00+05:30 1285.35 1286.00 1283.50 1284.65 267
Want to get/group 2H data starting from 9:00:00.
For 1H data I tried:
data.groupby(pd.Grouper(freq='1h')).agg({"open":"first","close":"last","low":"min","high":"max","volume":"sum"})
It appears result as follows:
open close low high volume
timestamp
2015-02-02 09:00:00+05:30 1280.00 1285.65 1280.00 1290.00 12620
2015-02-02 10:00:00+05:30 1285.65 1281.00 1281.00 1294.00 5103
2015-02-02 11:00:00+05:30 1281.00 1261.55 1261.20 1284.40 8995
2015-02-02 12:00:00+05:30 1261.30 1266.00 1256.00 1267.00 12772
2015-02-02 13:00:00+05:30 1266.00 1263.85 1260.00 1266.85 4076
2015-02-02 14:00:00+05:30 1261.35 1264.60 1260.00 1268.00 5937
2015-02-02 15:00:00+05:30 1263.00 1262.00 1256.65 1265.00 9505
2015-02-02 16:00:00+05:30 NaN NaN NaN NaN 0
2015-02-02 17:00:00+05:30 NaN NaN NaN NaN 0
How to remove NaN lines?
For 2H, in place of 1H, result:
open close low high volume
timestamp
2015-02-02 08:00:00+05:30 1280.00 1285.65 1280.00 1290.0 12620
2015-02-02 10:00:00+05:30 1285.65 1261.55 1261.20 1294.0 14098
2015-02-02 12:00:00+05:30 1261.30 1263.85 1256.00 1267.0 16848
2015-02-02 14:00:00+05:30 1261.35 1262.00 1256.65 1268.0 15442
2015-02-02 16:00:00+05:30 NaN NaN NaN NaN 0
2015-02-02 18:00:00+05:30 NaN NaN NaN NaN 0
Two problems: 1. NaN rows for outside trading hour. 2. Want it to start from 9AM.
This I think will work, but not sure:
data.groupby(pd.Grouper(freq='2h', offset='1h')).agg({"open":"first","close":"last","low":"min","high":"max","volume":"sum"})
If I try for a 7min or 7H timeframe (just to generalize) offset will not work properly, as 60min is not divisible by 7... It will work for 2min, 3min, 4min, 5min, 6min, etc... Any help, on how to generalize?