I have a financial time-series in 15 minute intervals.
2022-01-20 03:45:00+00:00 41941.738281
2022-01-20 04:00:00+00:00 41952.324219
2022-01-20 04:15:00+00:00 41945.421875
2022-01-20 04:30:00+00:00 41921.039062
2022-01-20 04:45:00+00:00 41910.382812
2022-01-20 05:00:00+00:00 41921.597656
2022-01-20 05:15:00+00:00 41914.296875
2022-01-20 05:30:00+00:00 41839.437500
2022-01-20 05:45:00+00:00 41873.421875
2022-01-20 06:00:00+00:00 41905.511719
2022-01-20 06:15:00+00:00 41958.675781
2022-01-20 06:30:00+00:00 42010.332031
2022-01-20 06:45:00+00:00 42045.492188
2022-01-20 07:00:00+00:00 42034.050781
2022-01-20 07:15:00+00:00 42010.828125
2022-01-20 07:30:00+00:00 41976.238281
2022-01-20 07:45:00+00:00 42078.578125
2022-01-20 08:00:00+00:00 42071.261719
2022-01-20 08:15:00+00:00 41995.722656
2022-01-20 08:26:00+00:00 41953.441406
Name: Open, dtype: float64
I want a summary of 6 hour intervals. I want the intervals to begin now. So first interval is from now until now - 6 hours.
I know about Grouper and the freq attribute:
df.groupby(pd.Grouper(level=0, freq="6h")).sum()
However this brings fixed intervals:
2022-01-19 12:00:00+00:00 1.011087e+06
2022-01-19 18:00:00+00:00 1.005388e+06
2022-01-20 00:00:00+00:00 1.005184e+06
2022-01-20 06:00:00+00:00 4.620401e+05
The date_range function can produce intervals I want.
pd.date_range(end="now", periods=4, freq="6h")
out:
DatetimeIndex(['2022-01-19 15:41:08.881073', '2022-01-19 21:41:08.881073',
'2022-01-20 03:41:08.881073', '2022-01-20 09:41:08.881073'],
dtype='datetime64[ns]', freq='6H')
How could I group by them.
Use parameter origin
in resample
for starting time from r
range like first value, if necessary filter by maximal (last) value of df.index
before:
r = pd.date_range(end="now", periods=4, freq="6h")
df[df.index <= r[-1]].resample('6h', origin=r[0]).sum()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.