大熊猫重新采样不规则间隔时间数据

时间:2017-09-26 19:26:47

标签: python pandas

                  x1         x1.resample('1T').mean
ts

2017-09-09 17:22:42   7.0        NaN
2017-09-09 17:22:53  11.0        NaN
2017-09-09 17:23:04   9.0        NaN
2017-09-09 17:23:15  15.0        NaN
2017-09-09 17:23:26  13.0        NaN
2017-09-09 17:23:38  19.0        NaN
2017-09-09 17:23:49  13.0        NaN
2017-09-09 17:24:00  15.0  10.666667

以上是df.x1Avg = df.x1.resample('1T').mean()的结果,当ts结束于hh:mm:00时,代码仅返回结果。我想要的结果是

                      x1         x1.resample('1T').mean
ts

2017-09-09 17:22:42   7.0        (7+11)/2
2017-09-09 17:22:53  11.0        (7+11)/2
2017-09-09 17:23:04   9.0        (9+15+13+19+13)/5
2017-09-09 17:23:15  15.0        (9+15+13+19+13)/5
2017-09-09 17:23:26  13.0        (9+15+13+19+13)/5
2017-09-09 17:23:38  19.0        (9+15+13+19+13)/5
2017-09-09 17:23:49  13.0        (9+15+13+19+13)/5
2017-09-09 17:24:00  15.0        15

1 个答案:

答案 0 :(得分:1)

您可以使用转换

df.index = pd.to_datetime(df.index)
df['mean'] = df.resample('1T').transform('mean')

你得到了

                     x1     mean
ts      
2017-09-09 17:22:42 7.0     9.0
2017-09-09 17:22:53 11.0    9.0
2017-09-09 17:23:04 9.0     13.8
2017-09-09 17:23:15 15.0    13.8
2017-09-09 17:23:26 13.0    13.8
2017-09-09 17:23:38 19.0    13.8
2017-09-09 17:23:49 13.0    13.8
2017-09-09 17:24:00 15.0    15.0