If I have a (possibly multidimensional) Python list where each element is one of True
, False
, or ma.masked
, what's the idiomatic way of turning this into a masked numpy array of bool?
Example:
>>> print(somefunc([[True, ma.masked], [False, True]]))
[[True --]
[False True]]
A masked array has to attributes,
data
andmask
:That starts without anything masked. Then as you suggest, assigning
np.ma.masked
to an element masks the slot:Here the
arr.mask
has been changed from scalarFalse
(applying to the whole array) to a boolean array ofFalse
, and then the selected item has been changed toTrue
.arr.data
hasn't changed:Looks like this change to
arr.mask
occurs indata.__setitem__
at:It checks if the assignment values is this special constant,
np.ma.masked
, and it makes the full mask, and assignsTrue
to an element.