Description
According to masked_invalid
docs, it is a shortcut for masked_where
, which says that copy=False
will modify a
in place.
But masked_where
didn't quite do this, and was recently changed in 1.21 to match what the docs actually say in #18967. While it seems to have propagated to e.g., masked_less
, it has not affected masked_invalid
, which continues to not modify a
.
Note though, that unlike masked_where
, a partially masked input array does not 'fix' copy=False
to match the docs, as in #18946. So changing this behaviour might be more problematic and it might be better to make this explicit in the docs instead.
Reproducing code example:
masked_where
modifies input:
In [1]: import numpy as np
In [25]: a = np.ma.array([np.inf, 1, 2, 3, 4])
In [26]: a
Out[26]:
masked_array(data=[inf, 1., 2., 3., 4.],
mask=False,
fill_value=1e+20)
In [28]: np.ma.masked_where(a == 3, a, copy=False)
Out[28]:
masked_array(data=[inf, 1.0, 2.0, --, 4.0],
mask=[False, False, False, True, False],
fill_value=1e+20)
In [29]: a
Out[29]:
masked_array(data=[inf, 1.0, 2.0, --, 4.0],
mask=[False, False, False, True, False],
fill_value=1e+20)
Other wrappers like masked_equal
modify input:
In [30]: a = np.ma.array([np.inf, 1, 2, 3, 4])
In [31]: np.ma.masked_equal(a, 3, copy=False)
Out[31]:
masked_array(data=[inf, 1.0, 2.0, --, 4.0],
mask=[False, False, False, True, False],
fill_value=3.0)
In [32]: a
Out[32]:
masked_array(data=[inf, 1.0, 2.0, --, 4.0],
mask=[False, False, False, True, False],
fill_value=1e+20)
But masked_invalid
does not:
In [33]: a = np.ma.array([np.inf, 1, 2, 3, 4])
In [34]: np.ma.masked_invalid(a, copy=False)
Out[34]:
masked_array(data=[--, 1.0, 2.0, 3.0, 4.0],
mask=[ True, False, False, False, False],
fill_value=1e+20)
In [35]: a
Out[35]:
masked_array(data=[inf, 1., 2., 3., 4.],
mask=False,
fill_value=1e+20)
And unlike masked_where
before, it does not magically work again for a partially masked array:
In [36]: a = np.ma.array([np.inf, 1, 2, 3, 4], mask=[False, False, False, True, False])
In [37]: a
Out[37]:
masked_array(data=[inf, 1.0, 2.0, --, 4.0],
mask=[False, False, False, True, False],
fill_value=1e+20)
In [38]: np.ma.masked_invalid(a, copy=False)
Out[38]:
masked_array(data=[--, 1.0, 2.0, --, 4.0],
mask=[ True, False, False, True, False],
fill_value=1e+20)
In [39]: a
Out[39]:
masked_array(data=[inf, 1.0, 2.0, --, 4.0],
mask=[False, False, False, True, False],
fill_value=1e+20)
NumPy/Python version information:
1.21.0 3.7.6 | packaged by conda-forge | (default, Mar 5 2020, 15:27:18)
[GCC 7.3.0]