[Numpy-discussion] empty_like for masked arrays

Aldcroft, Thomas aldcroft at head.cfa.harvard.edu
Mon Jun 10 16:17:36 EDT 2013

On Mon, Jun 10, 2013 at 3:47 PM, Nathaniel Smith <njs at pobox.com> wrote:

> Hi all,
> Is there anyone out there using numpy masked arrays, who has an
> opinion on how empty_like (and its friends ones_like, zeros_like)
> should handle the mask?
> Right now apparently if you call np.ma.empty_like on a masked array,
> you get a new masked array that shares the original array's mask, so
> modifying one modifies the other. That's almost certainly wrong. This
> PR:
>   https://github.com/numpy/numpy/pull/3404
> makes it so instead the new array has values that are all set to
> empty/zero/one, and a mask which is set to match the input array's
> mask (so whenever something was masked in the original array, the
> empty/zero/one in that place is also masked). We don't know if this is
> the desired behaviour for these functions, though. Maybe it's more
> intuitive for the new array to match the original array in shape and
> dtype, but to always have an empty mask. Or maybe not. None of us
> really use np.ma, so if you do and have an opinion then please speak
> up...

I use np.ma, and for me the most intuitive would be the second option where
the new array matches the original array in shape and dtype, but always has
an empty mask.  I always think of the *_like() functions as just copying
shape and dtype, so it would be a bit surprising to get part of the data
(the mask) from the original.  If you do need the mask then on the next
line you have an explicit statement to copy the mask and the code and
intent will be clear.  Also, most of the time the mask is set because that
particular data value was bad or missing, so it seems like it would be a
less-common use case to want a new empty array with the same mask.

My 2c,

> -n
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130610/9325b607/attachment.html>

More information about the NumPy-Discussion mailing list