Search code examples
pythonnumpyindexingmasked-array

calling `numpy.where` on a `masked_array`


Here's an example:

import numpy
from numpy import arange, where
from numpy.ma import masked_array

a = masked_array(arange(10), arange(10) < 5)
print(where((a <= 6))[0])

Expected output:

[5, 6]

Actual output:

[0, 1, 2, 3, 4, 5, 6]

How can I achieve the expected output? Thanks! :)


Solution

  • You simply need to use "numpy.ma.where" in order to handle the masked array:

    print(numpy.ma.where((a <= 6))[0])