diff options
author | Jonathan Helmus <jjhelmus@gmail.com> | 2015-10-08 17:40:11 -0500 |
---|---|---|
committer | Jonathan Helmus <jjhelmus@gmail.com> | 2015-10-12 14:10:20 -0500 |
commit | 6967f50dd1eb922cfa0e28956baa5a0bdc85331a (patch) | |
tree | 3fb649a7c83a4c8c1662c9ab25cc52dcdbbd97af | |
parent | 52912b5181f466bfb5ca29dca44c297f2def8281 (diff) | |
download | numpy-6967f50dd1eb922cfa0e28956baa5a0bdc85331a.tar.gz |
BUG: ma.put expands nomask
Previously when put was used on a MaskedArray with nomask the mask would be
incorrectly set to the mask of the values argument.
closes #6425
-rw-r--r-- | numpy/ma/core.py | 20 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 14 |
2 files changed, 25 insertions, 9 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 9e8dad08c..698416579 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -4399,7 +4399,6 @@ class MaskedArray(ndarray): [7 -- 30]] """ - m = self._mask # Hard mask: Get rid of the values/indices that fall on masked data if self._hardmask and self._mask is not nomask: mask = self._mask[indices] @@ -4411,16 +4410,19 @@ class MaskedArray(ndarray): self._data.put(indices, values, mode=mode) - if m is nomask: - m = getmask(values) + # short circut if neither self nor values are masked + if self._mask is nomask and getmask(values) is nomask: + return + + m = getmaskarray(self).copy() + + if getmask(values) is nomask: + m.put(indices, False, mode=mode) else: - m = m.copy() - if getmask(values) is nomask: - m.put(indices, False, mode=mode) - else: - m.put(indices, values._mask, mode=mode) - m = make_mask(m, copy=False, shrink=True) + m.put(indices, values._mask, mode=mode) + m = make_mask(m, copy=False, shrink=True) self._mask = m + return def ids(self): """ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 550f03131..f832ee60d 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -2671,6 +2671,20 @@ class TestMaskedArrayMethods(TestCase): assert_array_equal(x, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ]) assert_equal(x.mask, [1, 0, 0, 0, 1, 1, 0, 0, 0, 0]) + def test_put_nomask(self): + # GitHub issue 6425 + x = zeros(10) + z = array([3., -1.], mask=[False, True]) + + x.put([1, 2], z) + self.assertTrue(x[0] is not masked) + assert_equal(x[0], 0) + self.assertTrue(x[1] is not masked) + assert_equal(x[1], 3) + self.assertTrue(x[2] is masked) + self.assertTrue(x[3] is not masked) + assert_equal(x[3], 0) + def test_put_hardmask(self): # Tests put on hardmask d = arange(5) |