我想用另一个时间序列覆盖一个时间序列的值。输入序列在所有点都有值。覆盖时间序列将具有相同的索引(即日期),但我只想覆盖某些日期的值。我想指定它的方式是有一个时间序列,其中包含我想要覆盖到该值并且NaN
我不想应用覆盖的值。
也许最好用一个简单的例子来说明:
ints orts outts
index
2013-04-01 1 NaN 1
2013-05-01 2 11 2
2013-06-01 3 NaN 3
2013-07-01 4 9 4
2013-08-01 2 97 5
# should become
ints orts outts
index
2013-04-01 1 NaN 1
2013-05-01 2 11 11
2013-06-01 3 NaN 3
2013-07-01 4 9 9
2013-08-01 2 97 97
正如您从示例中看到的那样,我认为replace
orwhere
方法不会起作用,因为替换的值依赖于索引位置而不依赖于输入值。因为我想不止一次地这样做,所以我把它放在一个函数中,并且我确实有一个如下所示的解决方案:
def overridets(ts, orts):
tmp = pd.concat([ts, orts], join='outer', axis=1)
out = tmp.apply(lambda x: x[0] if pd.isnull(x[1]) else x[1], axis=1)
return out
问题是这运行速度相对较慢:在我的环境中,500 点系列需要 20 - 30 毫秒。将两个 500 点系列相乘大约需要 200 我们,所以我们谈论的速度要慢 100 倍。关于如何加快步伐的任何建议?
编辑
在@Andy 和@bmu 的帮助下,我对问题的最终解决方案如下:
def overridets(ts, orts):
ts.name = 'outts'
orts.name = 'orts'
tmp = pd.concat([ts, orts], join='outer', axis=1)
out = tmp['outts'].where(pd.isnull(tmp['orts']), tmp['orts'])
return out
我不需要inplace=True
,因为它总是包含在一个返回单个时间序列的函数中。快了近 50 倍,谢谢大家!