Skip to content
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

API: Ensure a full mask is returned for masked_invalid #22858

Merged
merged 1 commit into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion numpy/ma/core.py
Expand Up @@ -2357,7 +2357,12 @@ def masked_invalid(a, copy=True):

"""
a = np.array(a, copy=False, subok=True)
return masked_where(~(np.isfinite(a)), a, copy=copy)
res = masked_where(~(np.isfinite(a)), a, copy=copy)
# masked_invalid previously never returned nomask as a mask and doing so
# threw off matplotlib (gh-22842). So use shrink=False:
if res._mask is nomask:
res._mask = make_mask_none(res.shape, res.dtype)
return res

###############################################################################
# Printing options #
Expand Down
12 changes: 12 additions & 0 deletions numpy/ma/tests/test_core.py
Expand Up @@ -4519,6 +4519,18 @@ def __array__(self):
assert_array_equal(arr._data, np.array(Series()))
assert_array_equal(arr._mask, [False, True, True])

@pytest.mark.parametrize("copy", [True, False])
def test_masked_invalid_full_mask(self, copy):
# Matplotlib relied on masked_invalid always returning a full mask
# (Also astropy projects, but were ok with it gh-22720 and gh-22842)
a = np.ma.array([1, 2, 3, 4])
assert a._mask is nomask
res = np.ma.masked_invalid(a, copy=copy)
assert res.mask is not nomask
# mask of a should not be mutated
assert a.mask is nomask
assert np.may_share_memory(a._data, res._data) != copy

def test_choose(self):
# Test choose
choices = [[0, 1, 2, 3], [10, 11, 12, 13],
Expand Down