Ad

Pandas Index Interpolation Filling In Missing Values After The Last Data Point

Having a data frame with missing values at the end of a column, f.e.:

df = pd.DataFrame({'a':[np.nan,1,2,np.nan,np.nan,5,np.nan,np.nan]}, index=[0,1,2,3,4,5,6,7])

     a
0  NaN
1  1.0
2  2.0
3  NaN
4  NaN
5  5.0
6  NaN
7  NaN

Using 'index' interpolation method:

df.interpolate(method='index')

Returns the data frame with the last missing values forward filled:

     a
0  NaN
1  1.0
2  2.0
3  3.0
4  4.0
5  5.0
6  5.0
7  5.0

Is there a way to turn off that behaviour and leave the last missing values:

     a
0  NaN
1  1.0
2  2.0
3  3.0
4  4.0
5  5.0
6  NaN
7  NaN
Ad

Answer

I think need new parameter limit_direction in 0.23.0+, check this:

df = df.interpolate(method='index', limit=1, limit_direction='backward')
print (df)
     a
1  1.0
2  2.0
3  3.0
4  4.0
5  5.0
6  NaN
7  NaN

EDIT: If want replace NaNs only inside add parameter limit_area:

df = df.interpolate(method='index',limit_area='inside')
print (df)
     a
0  NaN
1  1.0
2  2.0
3  3.0
4  4.0
5  5.0
6  NaN
7  NaN
Ad
source: stackoverflow.com
Ad