您可以使用DatetimeIndex
重新采样高频数据(高达纳秒精度,警告:我相信这仅在即将发布的 0.13 版本中可用)。我已经成功地使用 pandas 重新采样了 24KHz 范围内的电生理数据。这是一个例子:
In [97]: index = date_range('1/1/2001 00:00:00', '1/1/2001 00:00:01', freq='22727N')
In [98]: index
Out[98]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2001-01-01 00:00:00, ..., 2001-01-01 00:00:00.999988]
Length: 44001, Freq: 22727N, Timezone: None
In [99]: s = Series(randn(index.size), index=index)
In [100]: s.head(10)
Out[100]:
2001-01-01 00:00:00 -0.820
2001-01-01 00:00:00.000022 -1.141
2001-01-01 00:00:00.000045 1.577
2001-01-01 00:00:00.000068 -1.031
2001-01-01 00:00:00.000090 0.343
2001-01-01 00:00:00.000113 -0.424
2001-01-01 00:00:00.000136 -0.753
2001-01-01 00:00:00.000159 0.411
2001-01-01 00:00:00.000181 0.238
2001-01-01 00:00:00.000204 1.048
Freq: 22727N, dtype: float64
In [101]: s.resample(s.index.freq * 4, how='mean')
Out[101]:
2001-01-01 00:00:00 -0.354
2001-01-01 00:00:00.000090 -0.106
2001-01-01 00:00:00.000181 0.245
2001-01-01 00:00:00.000272 0.568
2001-01-01 00:00:00.000363 0.047
2001-01-01 00:00:00.000454 -0.560
2001-01-01 00:00:00.000545 -0.485
2001-01-01 00:00:00.000636 -0.271
2001-01-01 00:00:00.000727 -0.457
2001-01-01 00:00:00.000818 0.078
2001-01-01 00:00:00.000909 0.394
2001-01-01 00:00:00.000999 0.185
2001-01-01 00:00:00.001090 -0.441
2001-01-01 00:00:00.001181 0.300
2001-01-01 00:00:00.001272 -0.521
...
2001-01-01 00:00:00.998715 -0.045
2001-01-01 00:00:00.998806 -0.044
2001-01-01 00:00:00.998897 0.090
2001-01-01 00:00:00.998988 0.748
2001-01-01 00:00:00.999078 -0.179
2001-01-01 00:00:00.999169 0.451
2001-01-01 00:00:00.999260 -1.041
2001-01-01 00:00:00.999351 -0.476
2001-01-01 00:00:00.999442 -0.234
2001-01-01 00:00:00.999533 -0.719
2001-01-01 00:00:00.999624 -0.606
2001-01-01 00:00:00.999715 -0.032
2001-01-01 00:00:00.999806 -0.296
2001-01-01 00:00:00.999897 -0.044
2001-01-01 00:00:00.999988 -0.951
Freq: 90908N, Length: 11001
您可以传入一个可调用的 to how
,这将允许您“做一些更智能的事情”。pandas
默认取给定时间段内的平均值(在这种情况下,这是 22727 个样本的每个块的平均值)。