Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: ma.masked_invalid(a, copy=False) does not modify a #19332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
QuLogic opened this issue Jun 24, 2021 · 1 comment Β· Fixed by #22046
Closed

BUG: ma.masked_invalid(a, copy=False) does not modify a #19332

QuLogic opened this issue Jun 24, 2021 · 1 comment Β· Fixed by #22046

Comments

@QuLogic
Copy link
Contributor

QuLogic commented Jun 24, 2021

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]
@WarrenWeckesser WarrenWeckesser added the component: numpy.ma masked arrays label Nov 18, 2021
@InessaPawson InessaPawson added the sprintable Issue fits the time-frame and setting of a sprint label Jan 6, 2022
@cmarmo
Copy link
Contributor

cmarmo commented Jul 22, 2022

Hello everybody,
I see this issue is labeled for sprints, may I have a look at it even outside a sprint?
If so, may I ask which solution is recommended by numpy developers?

So changing this behaviour might be more problematic and it might be better to make this explicit in the docs instead.

It would be better to clarify the documentation or make mask_invalid consistent with masked_where?
Thanks!

@InessaPawson InessaPawson changed the title ma.masked_invalid(a, copy=False) does not modify a BUG: ma.masked_invalid(a, copy=False) does not modify a Aug 24, 2022
@InessaPawson InessaPawson added 00 - Bug and removed sprintable Issue fits the time-frame and setting of a sprint labels Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants