下采样大熊猫的不规则时间序列

时间:2013-03-14 20:51:15

标签: python pandas

我在熊猫中有一个时间序列,如下所示:


2012-01-01 00:00:00.250000    12
2012-01-01 00:00:00.257000    34
2012-01-01 00:00:00.258000    45
2012-01-01 00:00:01.350000    56
2012-01-01 00:00:02.300000    78
2012-01-01 00:00:03.200000    89
2012-01-01 00:00:03.500000    90
2012-01-01 00:00:04.200000    12

有没有办法将其下采样到1秒数据而不对齐1秒边界?例如,有没有办法将这些数据输出(假设下采样的方式是使用采样时间之前或采样时间的最新值):


2012-01-01 00:00:00.250000    12
2012-01-01 00:00:01.250000    45
2012-01-01 00:00:02.250000    56
2012-01-01 00:00:03.250000    89
2012-01-01 00:00:04.250000    12

1 个答案:

答案 0 :(得分:5)

创建一个DateTimeIndex,频率为1秒,偏移量为四分之一秒,如此。

index = pd.date_range('2012-01-01 00:00:00.25', 
                      '2012-01-01 00:00:04.25', freq='S')

将您的数据与此索引相符合,并“填充”以按照您在所需结果中显示的方式进行下采样。<​​/ p>

s.reindex(index, method='ffill')
                            data
2012-01-01 00:00:00.250000    12
2012-01-01 00:00:01.250000    45
2012-01-01 00:00:02.250000    56
2012-01-01 00:00:03.250000    89
2012-01-01 00:00:04.250000    12