Skip to content

Commit

Permalink
Merge pull request #22858 from charris/backport-22849
Browse files Browse the repository at this point in the history
API: Ensure a full mask is returned for masked_invalid
  • Loading branch information
charris committed Dec 21, 2022
2 parents 0f1ad5a + af7ee55 commit 28e64c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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

0 comments on commit 28e64c4

Please sign in to comment.