I'm working with masked arrays thanks to some of the help I've gotten on stackoverflow, but I'm running into a problem with the np.where evaluation of a masked array.
My masked array is:
m_pt0 = np.ma.masked_array([1, 2, 3, 0, 4, 7, 6, 5],
mask=[False, True, False, False,
False, False, False, False])
And prints like this:
In [24]: print(m_pt0)
[1 -- 3 0 4 7 6 5]
And I'm looking for the index in m_pt0 where m_pt0 = 0, I would expect that
np.where(0 == m_pt0)
would return:
(array([3]))
However, despite the mask (or because of?), I instead get
(array([1, 3]),)
The entire point of using the mask is to avoid accessing indices that are "blank", so how can I use where (or another function) to only retrieve the indices that are unmasked and match my boolean criteria.
You need to use the masked variant of the where()
function, otherwise it will return wrong or unwanted results for masked arrays. The same goes for other functions, like polyfit()
.
I. e.:
In [2]: np.ma.where(0 == m_pt0)
Out[2]: (array([3]),)