Skip to content

Commit

Permalink
Merge pull request #22838 from seberg/issue-22826
Browse files Browse the repository at this point in the history
BUG: Do not use getdata() in np.ma.masked_invalid
  • Loading branch information
charris committed Dec 19, 2022
2 parents bf35f9b + 45cb037 commit 28329b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions numpy/ma/core.py
Expand Up @@ -2310,7 +2310,7 @@ def masked_values(x, value, rtol=1e-5, atol=1e-8, copy=True, shrink=True):
mask=False,
fill_value=2.1)
Unlike `masked_equal`, `masked_values` can perform approximate equalities.
Unlike `masked_equal`, `masked_values` can perform approximate equalities.
>>> ma.masked_values(x, 2.1, atol=1e-1)
masked_array(data=[1.0, 1.1, --, 1.1, 3.0],
Expand Down Expand Up @@ -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 #
Expand Down Expand Up @@ -2869,7 +2869,7 @@ def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
else:
# Case 2. : With a mask in input.
# If mask is boolean, create an array of True or False

# if users pass `mask=None` be forgiving here and cast it False
# for speed; although the default is `mask=nomask` and can differ.
if mask is None:
Expand Down Expand Up @@ -2922,7 +2922,7 @@ def _recursive_or(a, b):
else:
_data._mask = np.logical_or(mask, _data._mask)
_data._sharedmask = False

# Update fill_value.
if fill_value is None:
fill_value = getattr(data, '_fill_value', None)
Expand Down
14 changes: 14 additions & 0 deletions numpy/ma/tests/test_core.py
Expand Up @@ -4507,6 +4507,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],
Expand Down

0 comments on commit 28329b2

Please sign in to comment.