diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 0d5c73e7e467..e4ff8ef2d9f5 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -727,9 +727,8 @@ def _median(a, axis=None, out=None, overwrite_input=False): ind = np.meshgrid(*axes_grid, sparse=True, indexing='ij') # insert indices of low and high median - ind.insert(axis, h - 1) + ind.insert(axis, np.maximum(0, h - 1)) low = asorted[tuple(ind)] - low._sharedmask = False ind[axis] = h high = asorted[tuple(ind)] diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 27fac3d635a8..0a6de4ebaeef 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -790,6 +790,15 @@ def test_out(self): assert_equal(r, out) assert_(type(r) == MaskedArray) + def test_single_non_masked_value_on_axis(self): + data = [[1., 0.], + [0., 3.], + [0., 0.]] + masked_arr = np.ma.masked_equal(data, 0) + expected = [1., 3.] + assert_array_equal(np.ma.median(masked_arr, axis=0), + expected) + class TestCov(TestCase):