【发布时间】:2014-01-21 08:34:20
【问题描述】:
我正在尝试从 pandas TimeSeries 中删除所有“旧”值,例如所有超过 1 天的值(相对于最新值)。
天真地,我尝试了这样的事情:
from datetime import timedelta
def trim(series):
return series[series.index.max() - series.index < timedelta(days=1)]
给出错误:
TypeError: ufunc 'subtract' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'
很明显,问题出在这个表达式上:series.index.max() - series.index
然后我发现这是可行的:
def trim(series):
return series[series.index > series.index.max() - timedelta(days=1)]
有人能解释一下为什么后者会起作用,而前者会引发错误吗?
编辑:我使用的是熊猫版本 0.12.0
【问题讨论】:
-
请显示该系列;这需要 0.12(可能还有 0.13)才能获得适当的时间增量支持
-
@shx2 >.
标签: python pandas time-series timedelta