diff options
| author | Chiara Marmo <marmochiaskl@gmail.com> | 2022-07-25 14:00:02 -1000 |
|---|---|---|
| committer | Chiara Marmo <marmochiaskl@gmail.com> | 2022-07-25 15:41:39 -1000 |
| commit | 44c8da9f2caa5e09c78204e487cf74fe53c6d0d4 (patch) | |
| tree | 2590b7ffdab15cea2e156c4eaefd55e7c2e4030a /numpy/ma | |
| parent | 45bc13e6d922690eea43b9d807d476e0f243f836 (diff) | |
| download | numpy-44c8da9f2caa5e09c78204e487cf74fe53c6d0d4.tar.gz | |
Make mask_invalid consistent with mask_where when copy is set to False. Add test for type erroring.
Diffstat (limited to 'numpy/ma')
| -rw-r--r-- | numpy/ma/core.py | 17 | ||||
| -rw-r--r-- | numpy/ma/tests/test_core.py | 7 |
2 files changed, 11 insertions, 13 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 93eb74be3..405937046 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2356,20 +2356,11 @@ def masked_invalid(a, copy=True): fill_value=1e+20) """ - a = np.array(a, copy=copy, subok=True) - mask = getattr(a, '_mask', None) - if mask is not None: - condition = ~(np.isfinite(getdata(a))) - if mask is not nomask: - condition |= mask - cls = type(a) - else: - condition = ~(np.isfinite(a)) - cls = MaskedArray - result = a.view(cls) - result._mask = condition - return result + try: + return masked_where(~(np.isfinite(getdata(a))), a, copy=copy) + except TypeError: + raise ############################################################################### # Printing options # diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index b056d5169..5a6d642b4 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4496,6 +4496,13 @@ class TestMaskedArrayFunctions: assert_equal(ma, expected) assert_equal(ma.mask, expected.mask) + def test_masked_invalid_error(self): + a = np.arange(5, dtype=object) + a[3] = np.PINF + a[2] = np.NaN + with pytest.raises(TypeError, match="not supported for the input types"): + np.ma.masked_invalid(a) + def test_choose(self): # Test choose choices = [[0, 1, 2, 3], [10, 11, 12, 13], |
