Python pandas将即时小时数据重新采样到每日时间步,包括第二天00:00

时间:2016-10-01 17:48:03

标签: python pandas

我们说我有一系列瞬时温度测量值(即它们在准确的时刻捕获温度)。

index = pd.date_range('1/1/2000', periods=9, freq='T')
series = pd.Series(range(9), index=index)

series
Out[130]: 
2000-01-01 00:00:00    0
2000-01-01 06:00:00    1
2000-01-01 12:00:00    2
2000-01-01 18:00:00    3
2000-01-02 00:00:00    4
2000-01-02 06:00:00    5
2000-01-02 12:00:00    6
2000-01-02 18:00:00    7
2000-01-03 00:00:00    8
Freq: 6H, dtype: int64

我想得到平均每日温度。问题是我希望从当天和第二天的00:00:00包括当天的平均值。例如,我想平均2000-01-01 00:00:00到2000-01-02 00:00:00(含)。 pandas resample函数不会在bin中包含2000-01-02,因为它是不同的一天。

我认为在处理需要重新采样的瞬时测量时,这种情况经常出现。解决方案是什么?

1 个答案:

答案 0 :(得分:1)

设置

index = pd.date_range('1/1/2000', periods=9, freq='6H')
series = pd.Series(range(9), index=index)
series


2000-01-01 00:00:00    0
2000-01-01 06:00:00    1
2000-01-01 12:00:00    2
2000-01-01 18:00:00    3
2000-01-02 00:00:00    4
2000-01-02 06:00:00    5
2000-01-02 12:00:00    6
2000-01-02 18:00:00    7
2000-01-03 00:00:00    8
Freq: 6H, dtype: int64

解决方案

series.rolling(5).mean().resample('D').first()

2000-01-01    NaN
2000-01-02    2.0
2000-01-03    6.0
Freq: D, dtype: float64