Skip to content

Backport 7515, BUG: MaskedArray.count treats negative axes incorrectly #7793

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
Jun 30, 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
5 changes: 5 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4305,6 +4305,11 @@ def count(self, axis=None, keepdims=np._NoValue):
return self.size

axes = axis if isinstance(axis, tuple) else (axis,)
axes = tuple(a if a >= 0 else self.ndim + a for a in axes)
if len(axes) != len(set(axes)):
raise ValueError("duplicate value in 'axis'")
if np.any([a < 0 or a >= self.ndim for a in axes]):
raise ValueError("'axis' entry is out of bounds")
items = 1
for ax in axes:
items *= self.shape[ax]
Expand Down
8 changes: 7 additions & 1 deletion numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def test_count_func(self):
res = count(ott, 0)
assert_(isinstance(res, ndarray))
assert_(res.dtype.type is np.intp)
assert_raises(IndexError, ott.count, axis=1)
assert_raises(ValueError, ott.count, axis=1)

def test_minmax_func(self):
# Tests minimum and maximum.
Expand Down Expand Up @@ -4295,6 +4295,9 @@ def test_count(self):
assert_equal(count(a, keepdims=True), 16*ones((1,1,1)))
assert_equal(count(a, axis=1, keepdims=True), 2*ones((2,1,4)))
assert_equal(count(a, axis=(0,1), keepdims=True), 4*ones((1,1,4)))
assert_equal(count(a, axis=-2), 2*ones((2,4)))
assert_raises(ValueError, count, a, axis=(1,1))
assert_raises(ValueError, count, a, axis=3)

# check the 'nomask' path
a = np.ma.array(d, mask=nomask)
Expand All @@ -4305,6 +4308,9 @@ def test_count(self):
assert_equal(count(a, keepdims=True), 24*ones((1,1,1)))
assert_equal(count(a, axis=1, keepdims=True), 3*ones((2,1,4)))
assert_equal(count(a, axis=(0,1), keepdims=True), 6*ones((1,1,4)))
assert_equal(count(a, axis=-2), 3*ones((2,4)))
assert_raises(ValueError, count, a, axis=(1,1))
assert_raises(ValueError, count, a, axis=3)

# check the 'masked' singleton
assert_equal(count(np.ma.masked), 0)
Expand Down