【发布时间】:2018-10-08 11:31:59
【问题描述】:
假设我希望通过线性插值将时间序列重新索引为预定义索引,其中没有任何索引值在新旧索引之间共享。例如
# index is all precise timestamps e.g. 2018-10-08 05:23:07
series = pandas.Series(data,index)
# I want rounded date-times
desired_index = pandas.date_range("2010-10-08",periods=10,freq="30min")
Tutorials/API 建议这样做的方法是 reindex 然后使用 interpolate 填充 NaN 值。但是,由于新旧索引之间的日期时间没有重叠,因此 reindex 输出所有 NaN:
# The following outputs all NaN as no date times match old to new index
series.reindex(desired_index)
我不想在reindex 期间填充最接近的值,因为这会失去精度,所以我想出了以下内容;在插值之前将重新索引的系列与原始系列连接起来:
pandas.concat([series,series.reindex(desired_index)]).sort_index().interpolate(method="linear")
这似乎效率很低,将两个系列串联然后排序。有没有更好的办法?
【问题讨论】:
标签: python pandas time-series