Skip to content

Commit

Permalink
Merge pull request #22839 from charris/backport-22838
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 20, 2022
2 parents d337ba9 + b75d897 commit c690bcf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions numpy/ma/core.py
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
14 changes: 14 additions & 0 deletions numpy/ma/tests/test_core.py
Expand Up @@ -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],
Expand Down

0 comments on commit c690bcf

Please sign in to comment.