Skip to content

Commit 02b68f1

Browse files
authored
Merge pull request #22046 from cmarmo/masked-invalid
BUG: Make `mask_invalid` consistent with `mask_where` if `copy` is set to `False`
2 parents 110d75d + 4213779 commit 02b68f1

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

numpy/ma/core.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,20 +2356,8 @@ def masked_invalid(a, copy=True):
23562356
fill_value=1e+20)
23572357
23582358
"""
2359-
a = np.array(a, copy=copy, subok=True)
2360-
mask = getattr(a, '_mask', None)
2361-
if mask is not None:
2362-
condition = ~(np.isfinite(getdata(a)))
2363-
if mask is not nomask:
2364-
condition |= mask
2365-
cls = type(a)
2366-
else:
2367-
condition = ~(np.isfinite(a))
2368-
cls = MaskedArray
2369-
result = a.view(cls)
2370-
result._mask = condition
2371-
return result
23722359

2360+
return masked_where(~(np.isfinite(getdata(a))), a, copy=copy)
23732361

23742362
###############################################################################
23752363
# Printing options #

numpy/ma/tests/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,6 +4496,14 @@ def test_where_structured_masked(self):
44964496
assert_equal(ma, expected)
44974497
assert_equal(ma.mask, expected.mask)
44984498

4499+
def test_masked_invalid_error(self):
4500+
a = np.arange(5, dtype=object)
4501+
a[3] = np.PINF
4502+
a[2] = np.NaN
4503+
with pytest.raises(TypeError,
4504+
match="not supported for the input types"):
4505+
np.ma.masked_invalid(a)
4506+
44994507
def test_choose(self):
45004508
# Test choose
45014509
choices = [[0, 1, 2, 3], [10, 11, 12, 13],
@@ -5309,6 +5317,10 @@ def test_masked_array_no_copy():
53095317
a = np.ma.array([1, 2, 3, 4], mask=[1, 0, 0, 0])
53105318
_ = np.ma.masked_where(a == 3, a, copy=False)
53115319
assert_array_equal(a.mask, [True, False, True, False])
5320+
# check masked array with masked_invalid is updated in place
5321+
a = np.ma.array([np.inf, 1, 2, 3, 4])
5322+
_ = np.ma.masked_invalid(a, copy=False)
5323+
assert_array_equal(a.mask, [True, False, False, False, False])
53125324

53135325
def test_append_masked_array():
53145326
a = np.ma.masked_equal([1,2,3], value=2)

0 commit comments

Comments
 (0)