I was answering a question about pandas
interpolation
method. The OP wanted to use only interpolate where the number of consecutive np.nan
s was one. The limit=1
option for interpolate
will interpolate the first np.nan
and stop there. OP wanted to be able to tell that there were in fact more than one np.nan
and not even bother with the first one.
I boiled this down to just executing the interpolate
as is and mask the consecutive np.nan
after the fact.
The question is: What is a generalized solution that takes a 1-d array a
and an integer x
and produces a boolean mask with False in the positions of x or more consecutive np.nan
Consider the 1-d array a
a = np.array([1, np.nan, np.nan, np.nan, 1, np.nan, 1, 1, np.nan, np.nan, 1, 1])
I'd expect that for x = 2
the mask would look like this
# assume 1 for True and 0 for False
# a is [ 1. nan nan nan 1. nan 1. 1. nan nan 1. 1.]
# mask [ 1. 0. 0. 0. 1. 1. 1. 1. 0. 0. 1. 1.]
# ^
# |
# Notice that this is not masked because there is only one np.nan
I'd expect that for x = 3
the mask would look like this
# assume 1 for True and 0 for False
# a is [ 1. nan nan nan 1. nan 1. 1. nan nan 1. 1.]
# mask [ 1. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1.]
# ^ ^ ^
# | | |
# Notice that this is not masked because there is less than 3 np.nan's
I look forward to learning from others ideas ;-)