8000 Make np.ma.take works on scalars by eric-wieser · Pull Request #7586 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Make np.ma.take works on scalars #7586

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 2 commits into from
May 1, 2016
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
8 changes: 5 additions & 3 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5610,9 +5610,10 @@ def take(self, indices, axis=None, out=None, mode='raise'):
maskindices = getattr(indices, '_mask', nomask)
if maskindices is not nomask:
indices = indices.filled(0)
# Get the data
# Get the data, promoting scalars to 0d arrays with [...] so that
# .view works correctly
if out is None:
out = _data.take(indices, axis=axis, mode=mode).view(cls)
out = _data.take(indices, axis=axis, mode=mode)[...].view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
# Get the mask
Expand All @@ -5623,7 +5624,8 @@ def take(self, indices, axis=None, out=None, mode='raise'):
outmask = _mask.take(indices, axis=axis, mode=mode)
outmask |= maskindices
out.__setmask__(outmask)
return out
# demote 0d arrays back to scalars, for consistency with ndarray.take
return out[()]

# Array methods
copy = _arraymethod('copy')
Expand Down
4 changes: 4 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,10 @@ def test_take(self):
assert_equal(x.take([[0, 1], [0, 1]]),
masked_array([[10, 20], [10, 20]], [[0, 1], [0, 1]]))

# assert_equal crashes when passed np.ma.mask
Copy link
Member Author

Choose a reason for hiding this comment

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

This raises the question of whether any of these tests are actually verifying things, since assert_equal will ignore the mask

self.assertIs(x[1], np.ma.masked)
self.assertIs(x.take(1), np.ma.masked)

x = array([[10, 20, 30], [40, 50, 60]], mask=[[0, 0, 1], [1, 0, 0, ]])
assert_equal(x.take([0, 2], axis=1),
array([[10, 30], [40, 60]], mask=[[0, 1], [1, 0]]))
Expand Down
0