Pandas missing values : fill with the closest non NaN value

Clément F picture Clément F · Jun 27, 2017 · Viewed 8.3k times · Source

Assume I have a pandas series with several consecutive NaNs. I know fillna has several methods to fill missing values (backfill and fill forward), but I want to fill them with the closest non NaN value. Here's an example of what I have:

`s = pd.Series([0, 1, np.nan, np.nan, np.nan, np.nan, 3])`

And an example of what I want: s = pd.Series([0, 1, 1, 1, 3, 3, 3])

Does anyone know I could do that?

Thanks!

Answer

DSM picture DSM · Jun 27, 2017

You could use Series.interpolate with method='nearest':

In [11]: s = pd.Series([0, 1, np.nan, np.nan, np.nan, np.nan, 3])

In [12]: s.interpolate(method='nearest')
Out[12]: 
0    0.0
1    1.0
2    1.0
3    1.0
4    3.0
5    3.0
6    3.0
dtype: float64

In [13]: s = pd.Series([0, 1, np.nan, np.nan, 2, np.nan, np.nan, 3])

In [14]: s.interpolate(method='nearest')
Out[14]: 
0    0.0
1    1.0
2    1.0
3    2.0
4    2.0
5    2.0
6    3.0
7    3.0
dtype: float64