I want to fill missing values in my pandas series, if there are less than 3 consecutive NANs.
Original series with missing values:
s=pd.Series(pd.np.random.randn(20))
s[[1,3,5,7,12,13,14,15, 18]]=pd.np.nan
Gives:
0 0.444025
1 NaN
2 0.631753
3 NaN
4 -0.577121
5 NaN
6 1.299953
7 NaN
8 -0.252173
9 0.287641
10 0.941953
11 -1.624728
12 NaN
13 NaN
14 NaN
15 NaN
16 0.998952
17 0.195698
18 NaN
19 -0.788995
BUT, using pandas.fillna() with a limit only fills the # of values specified (not number of CONSECUTIVE NANs, as expected):
s.fillna(value=0, limit=3) #Fails to fill values at position 7 and forward
Desired output would fill NANs with 0 at positions 1,3,5,7, and 18. It would leave series of 4 NaNs in place in position 12-15.
The documentation and other posts on SO have not resolved this issue (e.g. here). Documentation seems to imply that this limit will work on consecutive NANs, not the overall # in entire dataset that will be filled. Thanks!