熊猫有几个选择。对于简单的统计,您可以在具有日期时间索引的 DataFrame/Series 上使用 resample 方法。
In [35]: ts
Out[35]:
2012-01-01 00:00:00 127
2012-01-01 00:00:01 452
2012-01-01 00:00:02 231
2012-01-01 00:00:03 434
2012-01-01 00:00:04 139
2012-01-01 00:00:05 223
2012-01-01 00:00:06 409
2012-01-01 00:00:07 101
2012-01-01 00:00:08 3
2012-01-01 00:00:09 393
2012-01-01 00:00:10 208
2012-01-01 00:00:11 416
2012-01-01 00:00:12 136
2012-01-01 00:00:13 343
2012-01-01 00:00:14 387
...
2012-01-01 00:01:25 307
2012-01-01 00:01:26 267
2012-01-01 00:01:27 199
2012-01-01 00:01:28 479
2012-01-01 00:01:29 423
2012-01-01 00:01:30 334
2012-01-01 00:01:31 442
2012-01-01 00:01:32 282
2012-01-01 00:01:33 289
2012-01-01 00:01:34 166
2012-01-01 00:01:35 4
2012-01-01 00:01:36 306
2012-01-01 00:01:37 165
2012-01-01 00:01:38 415
2012-01-01 00:01:39 316
Freq: S, Length: 100
In [37]: ts.resample('t', how='mean')
Out[37]:
2012-01-01 00:00:00 270.166667
2012-01-01 00:01:00 221.400000
Freq: T, dtype: float64
为了获得更大的灵活性,您可以hour
按时间戳对象的(或分钟、秒等)属性进行分组:
In [38]: g = ts.groupby(lambda x: x.minute)
In [39]: g
Out[39]: <pandas.core.groupby.SeriesGroupBy object at 0x107045150>
查看有关重采样的文档:http: //pandas.pydata.org/pandas-docs/dev/timeseries.html#up-and-downsampling