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

Skip to content

ENH: Masked array __array_function__ could "fix" e.g. np.append MA usage #22338

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

Open
peland opened this issue Sep 26, 2022 · 4 comments · May be fixed by #22913
Open

ENH: Masked array __array_function__ could "fix" e.g. np.append MA usage #22338

peland opened this issue Sep 26, 2022 · 4 comments · May be fixed by #22913
Labels
01 - Enhancement component: numpy.ma masked arrays Project Possible project, may require specific skills and long commitment

Comments

@peland
Copy link

peland commented Sep 26, 2022

Describe the issue:

if I append a masked array and something else using np.append, it returns a masked array but with the original mask replaced with False. I wouldn't mind if it returned a non-masked array (my fault!), but the current behaviour seems positively misleading! If it returns a masked array, it should surely preserve the mask.

Reproduce the code example:

import numpy as np
np.append(np.ma.masked_all(2),np.arange(2))

Error message:

No response

NumPy/Python version information:

1.20.3

Context for the issue:

No longer important for me (I corrected to np.ma.append), but could be misleading for others.

@seberg
Copy link
Member

seberg commented Sep 27, 2022

This is a general issue of masked array (currently/always). The only solution would be to implement __array_function__ probably.
The best solution is probably to have a masked array replacement that does not necessarily live within NumPy itself.

Implementing __array_function__ only to reject certain functions that are known to not work well (or forward them to the known masked versions) may well work. That is a bit harder outside NumPy currently, since we lack clear public API around it.

I am not seeing an issue, so will rename this one. I am not sure how easy/doable it is, but implementing an `array_function which normally just falls back to the builtin NumPy version may well be a good solution.

Note that masked arrays severely lack any manpower to actually work on such changes.

@seberg seberg changed the title BUG: append(ma,x) returns ma but with mask=False ENH: Masked array __array_function__ could "fix" e.g. np.append MA usage Sep 27, 2022
@MatteoRaso
Copy link
Contributor

It wouldn't be hard to slightly modify np.append and have it call np.ma.append if the input is masked, but I'm not sure if that's really necessary here, since we already have a function for appending masked arrays. A better option might be to add a warning about using np.append on masked arrays in the documentation.

@seberg
Copy link
Member

seberg commented Sep 28, 2022

It will be an uphill battle to convince anyone to add special paths for MA, which is why I am mentioning __array_function__ as the proper way to achieve the same thing.

Adding a note/warning about this to the masked array documentation seems like a good plan though, I could not find one after a brief search, so it is likely missing.

@seberg seberg added the Project Possible project, may require specific skills and long commitment label Sep 28, 2022
@peland
Copy link
Author

peland commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement component: numpy.ma masked arrays Project Possible project, may require specific skills and long commitment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants