Pandas Missing Values: Fill NeNN with the closest value

Suppose I have a pandas series with several consecutive NaNs. I know that fillna has several methods for filling in missing values ​​( backfill and fill forward ), but I want to fill them with the nearest value other than NaN. Here is 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 that I can do this?

Thanks!

+5
source share
1 answer

You can 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 
+8
source

Source: https://habr.com/ru/post/1269255/


All Articles