Skip to content

Commit 36f481b

Browse files
committed
BUG: change fix for gh-9328 to make masked.copy() a noop
1 parent de29dc5 commit 36f481b

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

numpy/ma/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6253,6 +6253,12 @@ def __iop__(self, other):
62536253
__iop__
62546254
del __iop__ # don't leave this around
62556255

6256+
def copy(self, *args, **kwargs):
6257+
""" Copy is a no-op on the maskedconstant, as it is a scalar """
6258+
# maskedconstant is a scalar, so copy doesn't need to copy. There's
6259+
# precedent for this with `np.bool_` scalars.
6260+
return self
6261+
62566262

62576263
masked = masked_singleton = MaskedConstant()
62586264
masked_array = MaskedArray

numpy/ma/tests/test_core.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4736,7 +4736,10 @@ def test_repr(self):
47364736
# copies should not exist, but if they do, it should be obvious that
47374737
# something is wrong
47384738
assert_equal(repr(np.ma.masked), 'masked')
4739-
assert_not_equal(repr(np.ma.masked.copy()), 'masked')
4739+
4740+
# create a new instance in a weird way
4741+
masked2 = np.ma.MaskedArray.__new__(np.ma.core.MaskedConstant)
4742+
assert_not_equal(repr(masked2), 'masked')
47404743

47414744
def test_pickle(self):
47424745
from io import BytesIO
@@ -4748,30 +4751,12 @@ def test_pickle(self):
47484751
res = pickle.load(f)
47494752
assert_(res is np.ma.masked)
47504753

4751-
def test_write_to_copy(self):
4754+
def test_copy(self):
47524755
# gh-9328
4753-
x = np.ma.masked.copy()
4754-
x[()] = 2
4755-
4756-
# write succeeds
4757-
assert_equal(x.data, 2)
4758-
assert_equal(x.mask, False)
4759-
4760-
# original should be unchanged
4761-
assert_equal(np.ma.masked.data, 0)
4762-
assert_equal(np.ma.masked.mask, True)
4763-
4764-
def test_no_copies(self):
4765-
# when making a view or a copy, downcast to MaskedArray
4766-
MC = np.ma.core.MaskedConstant
4767-
4768-
m_sl = np.ma.masked[...]
4769-
assert_equal(type(m_sl), np.ma.MaskedArray)
4770-
assert_equal(m_sl.mask, True)
4771-
4772-
m_copy = np.ma.masked.copy()
4773-
assert_equal(type(m_copy), np.ma.MaskedArray)
4774-
# assert_equal(m_copy.mask, True) - gh-9430
4756+
# copy is a no-op, like it is with np.True_
4757+
assert_equal(
4758+
np.ma.masked.copy() is np.ma.masked,
4759+
np.True_.copy() is np.True_)
47754760

47764761
def test_immutable(self):
47774762
assert_raises(ValueError, operator.setitem, np.ma.masked.data, (), 1)

0 commit comments

Comments
 (0)