我尝试使用pandas resample函数对每日TimeSeries
值进行上采样。当我进行一天的上采样(2012-01-01)时,我希望结果是上次采样所考虑的那天的平均值。结果应如下所示:
2012-01-01 -0,285344
Freq: D
然而,大熊猫会像这样返回两天:
2012-01-01 -0.412417
2012-01-02 0.127073
Freq: D
这是一个错误还是一个功能?如果它是一个功能,我如何设置重新采样参数以实现我的目标?
谢谢, 托马斯
以下是完整示例:
In [66]: rng = date_range('1/1/2012', periods=24, freq='H')
In [67]: ts = Series(np.random.randn(len(rng)), index=rng)
In [68]: ts
Out[68]:
2012-01-01 00:00:00 -0.412417
2012-01-01 01:00:00 0.442482
2012-01-01 02:00:00 1.321009
2012-01-01 03:00:00 0.104408
2012-01-01 04:00:00 1.124611
2012-01-01 05:00:00 -1.041293
2012-01-01 06:00:00 1.194104
2012-01-01 07:00:00 -0.249706
2012-01-01 08:00:00 1.927320
2012-01-01 09:00:00 -0.828365
2012-01-01 10:00:00 0.163760
2012-01-01 11:00:00 -0.736053
2012-01-01 12:00:00 -0.323408
2012-01-01 13:00:00 1.478162
2012-01-01 14:00:00 1.449437
2012-01-01 15:00:00 -1.114443
2012-01-01 16:00:00 -0.003780
2012-01-01 17:00:00 0.554562
2012-01-01 18:00:00 -2.019614
2012-01-01 19:00:00 0.463484
2012-01-01 20:00:00 0.862818
2012-01-01 21:00:00 -1.280048
2012-01-01 22:00:00 -0.137987
2012-01-01 23:00:00 -0.428777
Freq: H
In [69]: ts.resample('D')
Out[69]:
2012-01-01 -0.412417
2012-01-02 0.127073
Freq: D
答案 0 :(得分:2)
您可以使用loffset和closed选项获得所需的结果。
>>> ts.resample('D', how='mean', closed="left", loffset=datetime.timedelta(days=-1))
2012-01-01 0.200299