8000 BUG: Fix loss of dimensionality of np.ma.masked in ufunc by eric-wieser · Pull Request #8508 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix loss of dimensionality of np.ma.masked in ufunc #8508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6130,8 +6130,11 @@ def __new__(self):
def __array_finalize__(self, obj):
return

def __array_wrap__(self, obj):
return self
def __array_prepare__(self, obj, context=None):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this didn't error already when something tried to pass it a context

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)
Expand Down
22 changes: 22 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4425,6 +4425,28 @@ def test_count(self):
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]])
Expand Down
0