I have a 1D NumPy array that contains both floating point numbers and NaNs. There will almost always be multiple sequential NaNs at the start and end of the array with some NaNs throughout the middle of the array. There is no way to predict in advance the location or number of NaNs present in the array.
What I am trying to do:
- Retrieve indices of sequential NaNs occurring at the Start of the array and the End of the array only
- Not retrieve indices for NaNs in the middle of the array
- Avoid the use of loops/list comprehensions (if possible)
Below is a short example illustrating the type of array I am working with (actual arrays will have 1e3 to 1e9 elements).
data = np.array([NaN, NaN, NaN, 1.2, 3.4, 5.0, NaN, 3.3, 3.2, NaN, NaN, 4.0, NaN, NaN, NaN, NaN])
For this array I would like to retrieve the following indices:
idx = [0, 1, 2, 12, 13, 14, 15]