8000 BUG: masked-array median of 1d array should be scalar by AmitAronovitch · Pull Request #7592 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: masked-array median of 1d array should be scalar #7592

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 9 additions & 4 deletions numpy/ma/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,19 +654,21 @@ def _median(a, axis=None, out=None, overwrite_input=False):
axis += a.ndim

counts = asorted.shape[axis] - (asorted.mask).sum(axis=axis)
h = counts // 2
h = np.asarray(counts // 2)
# create indexing mesh grid for all but reduced axis
axes_grid = [np.arange(x) for i, x in enumerate(asorted.shape)
if i != axis]
ind = np.meshgrid(*axes_grid, sparse=True, indexing='ij')
# insert indices of low and high median
ind.insert(axis, h - 1)
ind[axis] = np.asarray(h-1)
low = asorted[ind]
low._sharedmask = False
if hasattr(low,"mask"):
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if isinstance(low, MaskedArray): I think.

low._sharedmask = False
ind[axis] = h
high = asorted[ind]
# duplicate high if odd number of elements so mean does nothing
odd = counts % 2 == 1
odd = np.asarray( counts % 2 == 1 )
if asorted.ndim == 1:
if odd:
low = high
Expand All @@ -676,7 +678,10 @@ def _median(a, axis=None, out=None, overwrite_input=False):
if np.issubdtype(asorted.dtype, np.inexact):
# avoid inf / x = masked
s = np.ma.sum([low, high], axis=0, out=out)
np.true_divide(s.data, 2., casting='unsafe', out=s.data)
if hasattr(s,"mask"):
Copy link
Member

Choose a reason for hiding this comment

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

isinstance here too

np.true_divide(s.data, 2., casting='unsafe', out=s.data)
else:
s = np.true_divide(s,2.0)
Copy link
Member

Choose a reason for hiding this comment

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

is it all right to simply write s = s/2 here?

else:
s = np.ma.mean([low, high], axis=0, out=out)
return s
Expand Down
13 changes: 13 additions & 0 deletions numpy/ma/tests/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,19 @@ def test_non_masked(self):
assert_equal(np.ma.median(np.arange(9)), 4.)
assert_equal(np.ma.median(range(9)), 4)

def test_masked_1d(self):
"test the examples given in the docstring of ma.median"
x = array(np.arange(8), mask=[0]*4 + [1]*4)
assert_equal(np.ma.median(x), 1.5)
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
x = array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
assert_equal(np.ma.median(x), 2.5)
assert_equal(np.ma.median(x).shape, (), "shape mismatch")

def test_1d_shape_consistency(self):
assert_equal(np.ma.median(array([1,2,3],mask=[0,0,0])).shape,
np.ma.median(array([1,2,3],mask=[0,1,0])).shape )

def test_2d(self):
# Tests median w/ 2D
(n, p) = (101, 30)
Expand Down
0