David Ferris - 1 year ago 67

Python Question

I have a numpy array with some

`NaN`

`arr = [ 0, NaN, 2, NaN, NaN, 5, 6, 7 ]`

Using some logic (outside of the question scope), I generate a mask of the NaN locations:

`mask = [ True, False, True, False, False, True, True, True ]`

I use this mask to select only the valid data:

`valid_arr = arr[mask] # [ 0, 2, 5, 6, 7 ]`

I then perform an arbitrary algorithm which selects several

`indeces`

`indeces = myAlgo(valid_arr) # [ 1, 3 ]`

The

`indeces`

`indeces`

`arr`

The array is time series data, not sorted. One solution is to iterate over the

`mask`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You can flat the mask which returns indices from the original array, and then use the new indices to subset the mask indices:

```
mask = np.array([ True, False, True, False, False, True, True, True ])
indices = [1,3]
np.flatnonzero(mask)[indices]
# array([2, 6])
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**