pythonpandasinterpolationpandas-resample

resample('D').interpolate() fills value, but resample('Y').interpolate() produces nans?


Let's start with two dates, two days apart, resample daily, and interpolate:

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

So far so good. Next, let's try doing it with two dates two years apart, and resample yearly:

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

Why do I get NaNs instead of [1., 1.5, 2.]?


Solution

  • Use the appropriate rule:

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

    or

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

    where 'AS' and 'YS' correspond to the start of the year.