我生成了一个5分钟时间序列的示例:
index = pd.date_range('1/1/2000', periods=10, freq='5T')
data=range(10)
ser = pd.Series(data, index=index)
它的外观:
^{pr2}$我需要什么
我想把这个时间序列变成一个15分钟的时间序列,每个15分钟的值都是15分钟内观察到的5分钟值的平均值,即
2000-01-01 00:15:00 2.0 # i.e. mean(1, 2, 3)
2000-01-01 00:30:00 5.0 # i.e. mean(4, 5, 6)
2000-01-01 00:45:00 8.0 # i.e. mean(7, 8, 9)
我尝试过的东西
如果我将这些数据重新采样到15分钟的桶中,然后调用意味着我得到:
ser.resample('15T').mean()
2000-01-01 00:00:00 1.0
2000-01-01 00:15:00 4.0
2000-01-01 00:30:00 7.0
2000-01-01 00:45:00 9.0
这不是我想要的计算方法。如果我在重采样调用中添加closed='right'
,我会更接近我想要的值,但是时间戳不正确。在
ser.resample('15T', closed='right').mean()
1999-12-31 23:45:00 0.0
2000-01-01 00:00:00 2.0
2000-01-01 00:15:00 5.0
2000-01-01 00:30:00 8.0
Freq: 15T, dtype: float64
有什么建议吗?在
您可以在resample中使用
label
参数这会将标签从重采样窗口的左侧(默认)移动到右侧。这比我有些笨拙的评论更简洁。在
相关问题 更多 >
编程相关推荐