From 4f2fa00cb8708bf36b642068c686065d2ce782aa Mon Sep 17 00:00:00 2001 From: Constanza Date: Sun, 9 May 2021 17:36:56 +0000 Subject: [PATCH 1/2] BUG: fixes 18946 --- numpy/ma/core.py | 4 ++++ numpy/ma/tests/test_core.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 14dc218047e5..8d5404305844 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1936,6 +1936,10 @@ def masked_where(condition, a, copy=True): result = a.view(cls) # Assign to *.mask so that structured masks are handled correctly. result.mask = _shrink_mask(cond) + # There is no view of a boolean so when 'a' is a MaskedArray with nomask the + # update to the result's mask has no effect. + if not copy and hasattr(a, '_mask') and getmask(a) is nomask: + a._mask = result._mask.view() return result diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 9bfb82d1ff1d..5ce130774246 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -5175,6 +5175,16 @@ def test_masked_array(): a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0]) assert_equal(np.argwhere(a), [[1], [3]]) +def test_masked_array_no_copy(): + # check nomask array is updated in place + a = np.ma.array([1, 2, 3, 4]) + _ = np.ma.masked_where(a==3, a, copy=False) + assert_array_equal(a.mask, [False, False, True, False]) + # check masked array is updated in place + a = np.ma.array([1, 2, 3, 4], mask=[1, 0, 0, 0]) + _ = np.ma.masked_where(a==3, a, copy=False) + assert_array_equal(a.mask, [True, False, True, False]) + def test_append_masked_array(): a = np.ma.masked_equal([1,2,3], value=2) b = np.ma.masked_equal([4,3,2], value=2) @@ -5213,7 +5223,6 @@ def test_append_masked_array_along_axis(): assert_array_equal(result.data, expected.data) assert_array_equal(result.mask, expected.mask) - def test_default_fill_value_complex(): # regression test for Python 3, where 'unicode' was not defined assert_(default_fill_value(1 + 1j) == 1.e20 + 0.0j) From 43ed940c6ef74a3f8dd26b731de2b96c9183767a Mon Sep 17 00:00:00 2001 From: Constanza Date: Sun, 9 May 2021 18:52:23 +0000 Subject: [PATCH 2/2] BUG: fix linter --- numpy/ma/core.py | 4 ++-- numpy/ma/tests/test_core.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 8d5404305844..63d42eccad08 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1936,8 +1936,8 @@ def masked_where(condition, a, copy=True): result = a.view(cls) # Assign to *.mask so that structured masks are handled correctly. result.mask = _shrink_mask(cond) - # There is no view of a boolean so when 'a' is a MaskedArray with nomask the - # update to the result's mask has no effect. + # There is no view of a boolean so when 'a' is a MaskedArray with nomask + # the update to the result's mask has no effect. if not copy and hasattr(a, '_mask') and getmask(a) is nomask: a._mask = result._mask.view() return result diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 5ce130774246..b71fa9069f60 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -5178,12 +5178,12 @@ def test_masked_array(): def test_masked_array_no_copy(): # check nomask array is updated in place a = np.ma.array([1, 2, 3, 4]) - _ = np.ma.masked_where(a==3, a, copy=False) - assert_array_equal(a.mask, [False, False, True, False]) + _ = np.ma.masked_where(a == 3, a, copy=False) + assert_array_equal(a.mask, [False, False, True, False]) # check masked array is updated in place a = np.ma.array([1, 2, 3, 4], mask=[1, 0, 0, 0]) - _ = np.ma.masked_where(a==3, a, copy=False) - assert_array_equal(a.mask, [True, False, True, False]) + _ = np.ma.masked_where(a == 3, a, copy=False) + assert_array_equal(a.mask, [True, False, True, False]) def test_append_masked_array(): a = np.ma.masked_equal([1,2,3], value=2)