考虑以下系列:
created_at
2014-01-27 21:50:05.040961 80000.00
2014-03-12 18:46:45.517968 79900.00
2014-09-05 20:54:17.991260 63605.31
2014-11-04 01:16:08.286631 64405.31
2014-11-04 01:17:26.398272 63605.31
2014-11-04 01:24:38.225306 64405.31
2014-11-13 19:32:14.273478 65205.31
Name: my_series, dtype: float64
我需要在一组特定的预定义日期(例如,从2014-12-01
to 2014-12-07
)。对于每个这样的样本,我想得到most原始系列中可用的最新值。
我一直在看resample http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html(也可以看看this https://stackoverflow.com/questions/17001389/pandas-resample-documentation?lq=1 and this https://stackoverflow.com/questions/26247301/causal-resampling-sum-over-the-last-x-time-unit?lq=1线程),因为它看起来是适合这项工作的工具。但是,我还没有很好地掌握该功能。
能resample
用于此?如果是这样,怎么办?
如果您首先定义一组预定义日期(days
在我下面的例子中),你可以reindex http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html并指定填充方法(“ffill”将向前传播最后一个有效观察,因此这意味着采用时间序列的最新观察值):
In [19]: s
Out[19]:
time
2014-01-27 21:50:05.040961 80000.00
2014-03-12 18:46:45.517968 79900.00
2014-09-05 20:54:17.991260 63605.31
2014-11-04 01:16:08.286631 64405.31
2014-11-04 01:17:26.398272 63605.31
2014-11-04 01:24:38.225306 64405.31
2014-11-13 19:32:14.273478 65205.31
Name: my_series, dtype: float64
In [20]: days = pd.date_range('2014-12-01', '2014-12-07')
In [21]: s.reindex(days, method='ffill')
Out[21]:
2014-12-01 65205.31
2014-12-02 65205.31
2014-12-03 65205.31
2014-12-04 65205.31
2014-12-05 65205.31
2014-12-06 65205.31
2014-12-07 65205.31
Freq: D, Name: my_series, dtype: float64
在这种情况下(您给出的示例日期),这给出了所有相同的值,因为对于所有日期,原始系列中的最新观察结果是相同的。
如果您不想给出特定的集合,而只想给出原始系列从开始到结束的所有日期,您可以使用resample
达到同样的效果:
In [23]: s.resample('D', how='last', fill_method='ffill')
Out[23]:
time
2014-01-27 80000
2014-01-28 80000
2014-01-29 80000
2014-01-30 80000
...
2014-11-10 64405.31
2014-11-11 64405.31
2014-11-12 64405.31
2014-11-13 65205.31
Freq: D, Name: my_series, Length: 291
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)