Tidy way to fill na -- with limit

Hi,

Is there a tidy way to limit the number of NAs that will be filled by the tidyr::fill() function? I am thinking of something that would parallel to the limit= argument in the python/pandas fillna() function

For example, in R, I may do the following:

df1 <- data.frame(
  id = 1:10,
  x = c(3, 4, 5, NA, NA, NA, NA, 3, 2, 1)
)

df1
#>    id  x
#> 1   1  3
#> 2   2  4
#> 3   3  5
#> 4   4 NA
#> 5   5 NA
#> 6   6 NA
#> 7   7 NA
#> 8   8  3
#> 9   9  2
#> 10 10  1

tidyr::fill(df1, x, .direction = "down")
#>    id x
#> 1   1 3
#> 2   2 4
#> 3   3 5
#> 4   4 5
#> 5   5 5
#> 6   6 5
#> 7   7 5
#> 8   8 3
#> 9   9 2
#> 10 10 1

Created on 2020-10-15 by the reprex package (v0.3.0)

Within Python, I can easily limit the fill, if I only want to fill ahead two. For example,


In [1]: import numpy as np 
   ...: import pandas as pd 
   ...:  
   ...: df1 = pd.DataFrame({ 
   ...:     "id": range(10), 
   ...:     "x" : [3, 4, 5, np.nan, np.nan, np.nan, np.nan, 3, 2, 1]}) 
   ...:                                                                                                                                                                            

In [2]: df1                                                                                                                                                                        
Out[2]: 
   id    x
0   0  3.0
1   1  4.0
2   2  5.0
3   3  NaN
4   4  NaN
5   5  NaN
6   6  NaN
7   7  3.0
8   8  2.0
9   9  1.0

In [3]: df1.fillna(method = "ffill", limit = 2)                                                                                                                                    
Out[3]: 
   id    x
0   0  3.0
1   1  4.0
2   2  5.0
3   3  5.0
4   4  5.0
5   5  NaN
6   6  NaN
7   7  3.0
8   8  2.0
9   9  1.0

Thanks!

Might be worth filing an issue over at the tidyr GitHub to request this feature. I could see it being useful and I don't think there is an elegant way to do this without some hacky workarounds.

1 Like