I was expecting to just say something like
ma.zeros(my_shape, mask=my_mask, hard_mask=True)
(where the mask is the correct shape) but ma.zeros (or ma.ones or ma.empty) rather surprisingly doesn't recognise the mask argument. The simplest I've come up with is
ma.array(np.zeros(my_shape), mask=my_mask, hard_mask=True)
which seems to involve unnecessary copying of lots of zeros. Is there a better way?
Make a masked array:
Modify
x, and see the result inM:The
M.datais aviewof the array used in creating it. No unnecessary copies.I haven't used functions like
np.ma.zeros, but_convert2mais a Python class, that takes afuncnameand returns new callable. It does not add mask-specific parameters. Study that yourself if necessary.np.ma.MaskedArray, the function that actually subclassesndarraytakes acopyparameterand the first line of its
__new__isI haven't quite sorted out whether
M._datais just a reference to the sourcedata, or aview. In either case, it isn't a copy, unless you say so.I haven't worked a lot with masked arrays, but my impression is that, while they can be convenient, they shouldn't be used where you are concerned about performance. There's a lot of extra work required to maintain both the
maskand thedata. The extra time involved in copying thedataarray, if any, will be minor.