# Buggy code
def _median(a, axis=None, out=None, overwrite_input=False):
    if overwrite_input:
        if axis is None:
            asorted = a.ravel()
            asorted.sort()
        else:
            a.sort(axis=axis)
            asorted = a
    else:
        asorted = sort(a, axis=axis)
    if axis is None:
        axis = 0
    elif axis < 0:
        axis += a.ndim

    if asorted.ndim == 1:
        idx, odd = divmod(count(asorted), 2)
        return asorted[idx - (not odd) : idx + 1].mean()

    counts = asorted.shape[axis] - (asorted.mask).sum(axis=axis)
    h = 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)
    low = asorted[tuple(ind)]
    low._sharedmask = False
    ind[axis] = h
    high = asorted[tuple(ind)]
    # duplicate high if odd number of elements so mean does nothing
    odd = counts % 2 == 1
    if asorted.ndim == 1:
        if odd:
            low = high
    else:
        low[odd] = high[odd]

    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.0, casting="unsafe", out=s.data)
    else:
        s = np.ma.mean([low, high], axis=0, out=out)
    return s


# Traceback (IndexError)
IndexError                                Traceback (most recent call last)
<ipython-input-26-b5117c1f3918> in <module>()
----> 1 np.ma.median(np.ma.array(np.ones((5, 5))), axis=0)

C:\-\lib\site-packages\numpy\ma\extras.py in median(a, axis, out, overwrite_input, keepdims)
693
694     r, k = _ureduce(a, func=_median, axis=axis, out=out,
--> 695                     overwrite_input=overwrite_input)
696     if keepdims:
697         return r.reshape(k)

C:\-\lib\site-packages\numpy\lib\function_base.py in _ureduce(a, func, **kwargs)
3631         keepdim = [1] * a.ndim
3632
-> 3633     r = func(a, **kwargs)
3634     return r, keepdim
3635

C:\-\lib\site-packages\numpy\ma\extras.py in _median(a, axis, out, overwrite_input)
736             low = high
737     else:
--> 738         low[odd] = high[odd]
739
740     if np.issubdtype(asorted.dtype, np.inexact):

C:\-\lib\site-packages\numpy\ma\core.py in __getitem__(self, indx)
3161
3162         """
-> 3163         dout = self.data[indx]
3164         # We could directly use ndarray.__getitem__ on self.
3165         # But then we would have to modify __array_finalize__ to prevent the

IndexError: in the future, 0-d boolean arrays will be interpreted as a valid boolean index

# Fixed code
def _median(a, axis=None, out=None, overwrite_input=False):
    if overwrite_input:
        if axis is None:
            asorted = a.ravel()
            asorted.sort()
        else:
            a.sort(axis=axis)
            asorted = a
    else:
        asorted = sort(a, axis=axis)

    if axis is None:
        axis = 0
    elif axis < 0:
        axis += asorted.ndim

    if asorted.ndim == 1:
        idx, odd = divmod(count(asorted), 2)
        return asorted[idx + odd - 1 : idx + 1].mean(out=out)

    counts = count(asorted, axis=axis)
    h = 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)
    low = asorted[tuple(ind)]
    low._sharedmask = False
    ind[axis] = h
    high = asorted[tuple(ind)]

    # duplicate high if odd number of elements so mean does nothing
    odd = counts % 2 == 1
    if asorted.ndim > 1:
        np.copyto(low, high, where=odd)
    elif odd:
        low = high

    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.0, casting="unsafe", out=s.data)
    else:
        s = np.ma.mean([low, high], axis=0, out=out)
    return s