8000 BUG: fix ma.median for empty ndarrays by juliantaylor · Pull Request #8707 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fix ma.median for empty ndarrays #8707

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
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: 7 additions & 0 deletions numpy/ma/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,13 @@ def _median(a, axis=None, out=None, overwrite_input=False):
elif axis < 0:
axis += asorted.ndim

if asorted.shape[axis] == 0:
# for empty axis integer indices fail so use slicing to get same result
# as median (which is mean of empty slice = nan)
indexer = [slice(None)] * asorted.ndim
indexer[axis] = slice(0, 0)
return np.ma.mean(asorted[indexer], axis=axis, out=out)

if asorted.ndim == 1:
counts = count(asorted)
idx, odd = divmod(count(asorted), 2)
Expand Down
6 changes: 3 additions & 3 deletions numpy/ma/tests/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,14 +1044,14 @@ def test_empty(self):

# axis 0 and 1
b = np.ma.masked_array(np.array([], dtype=float, ndmin=2))
assert_equal(np.median(a, axis=0), b)
assert_equal(np.median(a, axis=1), b)
assert_equal(np.ma.median(a, axis=0), b)
assert_equal(np.ma.median(a, axis=1), b)

# axis 2
b = np.ma.masked_array(np.array(np.nan, dtype=float, ndmin=2))
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_equal(np.median(a, axis=2), b)
assert_equal(np.ma.median(a, axis=2), b)
assert_(w[0].category is RuntimeWarning)

def test_object(self):
Expand Down
0