From b75d89733068aa1c3d1008e955f1edf6a4160982 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 19 Dec 2022 20:20:09 +0100 Subject: [PATCH] BUG: Do not use getdata() in np.ma.masked_invalid This is the minimal solution to fix gh-22826 with as little change as possible. We should fix `getdata()` but I don't want to do that in a bug-fix release really. IMO the alternative is to revert gh-22046 which would also revert the behavior noticed in gh-22720 (which seems less harmful though). Closes gh-22826 --- numpy/ma/core.py | 4 ++-- numpy/ma/tests/test_core.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index be213ebf3b21..3ca6e1a23284 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2356,8 +2356,8 @@ def masked_invalid(a, copy=True): fill_value=1e+20) """ - - return masked_where(~(np.isfinite(getdata(a))), a, copy=copy) + a = np.array(a, copy=False, subok=True) + return masked_where(~(np.isfinite(a)), a, copy=copy) ############################################################################### # Printing options # diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 028f64c61381..96864da5db66 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4505,6 +4505,20 @@ def test_masked_invalid_error(self): match="not supported for the input types"): np.ma.masked_invalid(a) + def test_masked_invalid_pandas(self): + # getdata() used to be bad for pandas series due to its _data + # attribute. This test is a regression test mainly and may be + # removed if getdata() is adjusted. + class Series(): + _data = "nonsense" + + def __array__(self): + return np.array([5, np.nan, np.inf]) + + arr = np.ma.masked_invalid(Series()) + assert_array_equal(arr._data, np.array(Series())) + assert_array_equal(arr._mask, [False, True, True]) + def test_choose(self): # Test choose choices = [[0, 1, 2, 3], [10, 11, 12, 13],