diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-01-20 18:00:59 +0000 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-01-20 22:52:35 +0000 |
commit | c1e66e4caf1a102a5f3f27456f4d683d4c38c2d0 (patch) | |
tree | 0db1899b160bb51f6cf50fbb3b6eb251f08c2133 | |
parent | 20d6ca1bd0c23cf22880ea72ae7abb4b67d41894 (diff) | |
download | numpy-c1e66e4caf1a102a5f3f27456f4d683d4c38c2d0.tar.gz |
BUG: Fix loss of dimensionality of np.ma.masked in ufunc
Fixes #8505
-rw-r--r-- | numpy/ma/core.py | 7 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 22 |
2 files changed, 27 insertions, 2 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 4466dc0af..49c790d4f 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -6130,8 +6130,11 @@ class MaskedConstant(MaskedArray): def __array_finalize__(self, obj): return - def __array_wrap__(self, obj): - return self + def __array_prepare__(self, obj, context=None): + return self.view(MaskedArray).__array_prepare__(obj, context) + + def __array_wrap__(self, obj, context=None): + return self.view(MaskedArray).__array_wrap__(obj, context) def __str__(self): return str(masked_print_option._display) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index b5be24b31..c6f276211 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4425,6 +4425,28 @@ class TestOptionalArgs(TestCase): assert_raises(ValueError, count, np.ma.array(1), axis=1) +class TestMaskedConstant(TestCase): + def _do_add_test(self, add): + # sanity check + self.assertIs(add(np.ma.masked, 1), np.ma.masked) + + # now try with a vector + vector = np.array([1, 2, 3]) + result = add(np.ma.masked, vector) + + # lots of things could go wrong here + assert_(result is not np.ma.masked) + assert_(not isinstance(result, np.ma.core.MaskedConstant)) + assert_equal(result.shape, vector.shape) + assert_equal(np.ma.getmask(result), np.ones(vector.shape, dtype=bool)) + + def test_ufunc(self): + self._do_add_test(np.add) + + def test_operator(self): + self._do_add_test(lambda a, b: a + b) + + def test_masked_array(): a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0]) assert_equal(np.argwhere(a), [[1], [3]]) |