让我们从两个日期开始,相隔两天,每天重新采样,然后进行插值:
In [1]: ts = pd.Series([1, 2], index=pd.DatetimeIndex(['1950-01-01', '1950-01-03']))
In [2]: ts.resample('D').interpolate()
Out[2]:
1950-01-01 1.0
1950-01-02 1.5
1950-01-03 2.0
Freq: D, dtype: float64
到目前为止一切顺利。接下来,让我们尝试用相隔两年的两个日期来做,并每年重新采样:
In [3]: ts = pd.Series([1, 2], index=pd.DatetimeIndex(['1950-01-01', '1952-01-01']))
In [4]: ts.resample('Y').interpolate()
Out[4]:
1950-12-31 NaN
1951-12-31 NaN
1952-12-31 NaN
Freq: A-DEC, dtype: float64
为什么我得到的是 NaN 而不是
[1., 1.5, 2.]
?
使用适当的
rule
:
ts.resample('AS').interpolate()
或
ts.resample('YS').interpolate()
其中
'AS'
和'YS'
对应于年初。