diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2022-12-21 10:21:43 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-21 10:21:43 -0500 |
commit | 2f64274d360b25d509636897d5d39157080a42cc (patch) | |
tree | a6ae03d230060f98f91cd445564df04f60d4b41b | |
parent | 1d2551e34ae8f601b6c5f43b30cf3ba25c517724 (diff) | |
parent | 954aee7ab016d6f76d263bfe4c70de114eed63eb (diff) | |
download | numpy-2f64274d360b25d509636897d5d39157080a42cc.tar.gz |
Merge pull request #22849 from seberg/issue-22720
API: Ensure a full mask is returned for masked_invalid
-rw-r--r-- | numpy/ma/core.py | 7 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 12 |
2 files changed, 18 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 0038d2d6e..90f852e5c 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -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 # diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 3d48de8b9..0a194fd37 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4521,6 +4521,18 @@ class TestMaskedArrayFunctions: 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], |