我有一组定期测量的值.说:
I have one set of values measured at regular times. Say:
import pandas as pd import numpy as np rng = pd.date_range('2013-01-01', periods=12, freq='H') data = pd.Series(np.random.randn(len(rng)), index=rng)例如,还有另一组更多的任意时间(实际上,这些时间不是常规序列)
And another set of more arbitrary times, for example, (in reality these times are not a regular sequence)
ts_rng = pd.date_range('2013-01-01 01:11:21', periods=7, freq='87Min') ts = pd.Series(index=ts_rng)我想知道以ts为单位的时间插值的数据的值. 我可以在numpy中做到这一点:
I want to know the value of data interpolated at the times in ts. I can do this in numpy:
x = np.asarray(ts_rng,dtype=np.float64) xp = np.asarray(data.index,dtype=np.float64) fp = np.asarray(data) ts[:] = np.interp(x,xp,fp)但是我感到熊猫在resample,reindex等位置中具有此功能,但是我不太了解.
But I feel pandas has this functionality somewhere in resample, reindex etc. but I can't quite get it.
推荐答案您可以串联两个时间序列并按索引排序.由于第二个系列中的值是NaN,因此您可以interpolate并且只需选择代表第二个系列中的点的值即可:
You can concatenate the two time series and sort by index. Since the values in the second series are NaN you can interpolate and the just select out the values that represent the points from the second series:
pd.concat([data, ts]).sort_index().interpolate().reindex(ts.index)或
pd.concat([data, ts]).sort_index().interpolate()[ts.index]更多推荐
在 pandas 中将一个时间序列插值到另一个时间序列
发布评论