Replace NaN with the closest value in a series of non-numeric objects?

I use Pandas and Numpy, and I'm trying to replace all the NaN values ​​in a series like this:

date                    a
2017-04-24 01:00:00  [1,0,0]
2017-04-24 01:20:00  [1,0,0]
2017-04-24 01:40:00  NaN
2017-04-24 02:00:00  NaN
2017-04-24 02:20:00  [0,1,0]
2017-04-24 02:40:00  [1,0,0]
2017-04-24 03:00:00  NaN
2017-04-24 03:20:00  [0,0,1]
2017-04-24 03:40:00  NaN
2017-04-24 04:00:00  [1,0,0]

with the closest objcet (Numpy array in this case). Result:

date                    a
2017-04-24 01:00:00  [1,0,0]
2017-04-24 01:20:00  [1,0,0]
2017-04-24 01:40:00  [1,0,0]
2017-04-24 02:00:00  [0,1,0]
2017-04-24 02:20:00  [0,1,0]
2017-04-24 02:40:00  [1,0,0]
2017-04-24 03:00:00  [1,0,0]
2017-04-24 03:20:00  [0,0,1]
2017-04-24 03:40:00  [0,0,1]
2017-04-24 04:00:00  [1,0,0]

Does anyone know an effective method? Many thanks.

+4
source share
1 answer

drop nulls and then fill reindex

df.set_index('date').a.dropna().reindex(df.date, method='nearest').reset_index()

                 date          a
0 2017-04-24 01:00:00  [1, 0, 0]
1 2017-04-24 01:20:00  [1, 0, 0]
2 2017-04-24 01:40:00  [1, 0, 0]
3 2017-04-24 02:00:00  [0, 1, 0]
4 2017-04-24 02:20:00  [0, 1, 0]
5 2017-04-24 02:40:00  [1, 0, 0]
6 2017-04-24 03:00:00  [0, 0, 1]
7 2017-04-24 03:20:00  [0, 0, 1]
8 2017-04-24 03:40:00  [1, 0, 0]
9 2017-04-24 04:00:00  [1, 0, 0]
+7
source

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


All Articles