Skip to content
Advertisement

ValueError: Invalid fill method. Expecting pad (ffill) or backfill (bfill). Got nearest

I have this df:

          Week     U.S. 30 yr FRM   U.S. 15 yr FRM
0     2014-12-31        3.87          3.15
1     2015-01-01        NaN           NaN
2     2015-01-02        NaN           NaN
3     2015-01-03        NaN           NaN
4     2015-01-04        NaN           NaN
... ... ... ...
2769    2022-07-31      NaN           NaN
2770    2022-08-01      NaN           NaN
2771    2022-08-02      NaN           NaN
2772    2022-08-03      NaN           NaN
2773    2022-08-04      4.99          4.26

And when I try to run this interpolation: pmms_df.interpolate(method = 'nearest', inplace = True)

I get ValueError: Invalid fill method. Expecting pad (ffill) or backfill (bfill). Got nearest

I read in this post that pandas interpolate doesn’t do well with the time columns, so I tried this: pmms_df[['U.S. 30 yr FRM', 'U.S. 15 yr FRM']].interpolate(method = 'nearest', inplace = True)

but the output is exactly the same as before the interpolation.

Advertisement

Answer

It may not work great with date columns, but it works well with a datetime index, which is probably what you should be using here:

df = df.set_index('Week')
df = df.interpolate(method='nearest')
print(df)

# Output:
            U.S. 30 yr FRM  U.S. 15 yr FRM
Week
2014-12-31            3.87            3.15
2015-01-01            3.87            3.15
2015-01-02            3.87            3.15
2015-01-03            3.87            3.15
2015-01-04            3.87            3.15
2022-07-31            4.99            4.26
2022-08-01            4.99            4.26
2022-08-02            4.99            4.26
2022-08-03            4.99            4.26
2022-08-04            4.99            4.26
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement