[Numpy-discussion] Changing MaskedArray.squeeze() to never return masked

Eric Wieser wieser.eric+numpy at gmail.com
Tue Jul 18 09:37:45 EDT 2017


When using ndarray.squeeze, a view is returned, which means you can do the
follow (somewhat-contrived) operation:

>>> def fill_contrived(a):
        a.squeeze()[...] = 2
        return a
>>> fill_contrived(np.array([1]))
array(2)

However, when tried with a masked array, this can fail, breaking liskov
subsitution:

>>> fill_contrived(np.ma.array([1], mask=[True]))
MaskError: Cannot alter the masked element.

This fails because squeeze breaks the contract of returning a view, instead
deciding sometimes to return masked.

There is a patch that fixes this in gh-9432
<https://github.com/numpy/numpy/pull/9432> - however, by necessity it
breaks any existing code that uses m_arr.squeeze() is np.ma.masked.

Is this too breaking a change?

Eric
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170718/5a7c2ce2/attachment-0001.html>


More information about the NumPy-Discussion mailing list