resample('D').interpolate() 填充值,但 resample('Y').interpolate() 产生 nans?

问题描述 投票:0回答:1

让我们从两个日期开始,相隔两天,每天重新采样,然后进行插值:

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.]

python pandas interpolation pandas-resample
1个回答
2
投票

使用适当的

rule

ts.resample('AS').interpolate()

ts.resample('YS').interpolate()

其中

'AS'
'YS'
对应于年初。

© www.soinside.com 2019 - 2024. All rights reserved.