我想通过使用带有“mean”方法的 pandas .resample 函数将 15 分钟数据重新采样为 60 分钟数据,但默认情况下,此方法采用上午 9:00、上午 9:15、上午 9:30 的平均值,和 9:00 AM 时间戳的 9:45 AM 值(作为示例)。有没有办法取 9:00 AM 时间戳的 8:15 AM、8:30 AM、8:45 AM 和 9:00 AM 值的平均值?
输入数据:
Generated On CB_P
2019-01-01 08:15:00+00:00 0.187
2019-01-01 08:30:00+00:00 0.228
2019-01-01 08:45:00+00:00 0.242
2019-01-01 09:00:00+00:00 0.8270
2019-01-01 09:15:00+00:00 1.083
2019-01-01 09:30:00+00:00 3.022
2019-01-01 09:45:00+00:00 1.511
2019-01-01 10:00:00+00:00 1.568
2019-01-01 10:15:00+00:00 6.365
2019-01-01 10:30:00+00:00 8.23
2019-01-01 10:45:00+00:00 9.3
2019-01-01 11:00:00+00:00 14.311
2019-01-01 11:15:00+00:00 13.045
2019-01-01 11:30:00+00:00 11.05
2019-01-01 11:45:00+00:00 11.257
2019-01-01 12:00:00+00:00 13.367
2019-01-01 12:15:00+00:00 11.895
2019-01-01 12:30:00+00:00 9.245
2019-01-01 12:45:00+00:00 7.254
2019-01-01 13:00:00+00:00 15.773
2019-01-01 13:15:00+00:00 14.280
2019-01-01 13:30:00+00:00 17.258
2019-01-01 13:45:00+00:00 7.792
2019-01-01 14:00:00+00:00 6.893
2019-01-01 14:15:00+00:00 4.693
2019-01-01 14:30:00+00:00 4.271
2019-01-01 14:45:00+00:00 1.524
2019-01-01 15:00:00+00:00 1.495
2019-01-01 15:15:00+00:00 1.03
2019-01-01 15:30:00+00:00 0.364
2019-01-01 15:45:00+00:00 0.045
预期输出:
Generated On CB_P
2019-01-01 09:00:00+00:00 0.371
2019-01-01 10:00:00+00:00 1.796
2019-01-01 11:00:00+00:00 9.5515
2019-01-01 12:00:00+00:00 12.180
2019-01-01 13:00:00+00:00 11.04
2019-01-01 14:00:00+00:00 11.556
2019-01-01 15:00:00+00:00 2.996
偏移值可用于重采样。请参考这个。
df['Generated On'] = pd.to_datetime(df['Generated On'])
df.set_index('Generated On', inplace=True)
df2 = df.resample('1H', offset=15).mean()
df2.index = df2.index.strftime('%Y-%m-%d %H:%M:%S')
df2
CB_P
Generated On
2019-01-01 08:00:00 0.371000
2019-01-01 09:00:00 1.796000
2019-01-01 10:00:00 9.551500
2019-01-01 11:00:00 12.179750
2019-01-01 12:00:00 11.041750
2019-01-01 13:00:00 11.555750
2019-01-01 14:00:00 2.995750
2019-01-01 15:00:00 0.479667
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句