Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas missing values : fill with the closest non NaN value

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!

like image 205
Clément F Avatar asked Jun 27 '17 14:06

Clément F


1 Answers

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
like image 86
DSM Avatar answered Oct 04 '22 00:10

DSM