# [SciPy-User] scipy.stats.nanmedian

josef.pktd@gmai... josef.pktd@gmai...
Thu Jan 21 20:15:59 CST 2010

```On Thu, Jan 21, 2010 at 8:54 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> I noticed a couple of issues with nanmedian in scipy.stats:
>
>>> from scipy.stats import nanmedian
>>> nanmedian(1)
> ---------------------------------------------------------------------------
> ValueError: axis must be less than arr.ndim; axis=0, rank=0.
>>> nanmedian(True)
> ---------------------------------------------------------------------------
> ValueError: axis must be less than arr.ndim; axis=0, rank=0.
>>> nanmedian(np.array(1))
> ---------------------------------------------------------------------------
> ValueError: axis must be less than arr.ndim; axis=0, rank=0.
>>> nanmedian(np.array([1, 2, 3]))
>   array(2.0)
>
> Changing the function from the original:
>
> def nanmedian(x, axis=0):
>    x, axis = _chk_asarray(x,axis)
>    x = x.copy()
>    return np.apply_along_axis(_nanmedian,axis,x)
>
> to this (I know, it is not pretty):
>
> def nanmedian(x, axis=0):
>    if np.isscalar(x):
>        return float(x)
>    x, axis = _chk_asarray(x, axis)
>    if x.ndim == 0:
>        return float(x.tolist())
>    x = x.copy()
>    x = np.apply_along_axis(_nanmedian, axis, x)
>    if x.ndim == 0:
>        x = float(x.tolist())
>    return x

Can you open a ticket, so that I don't forget to look at it?

I will need to play with it. There are some things that I don't
understand right away.
What's the difference between isscalar and ndim=0 ?
Why do you have the tolist()

Thanks,
Josef
> gives the expected results:
>
>>> nanmedian(1)
>   1.0
>>> nanmedian(True)
>   1.0
>>> nanmedian(np.array(1))
>   1.0
>>> nanmedian(np.array([1, 2, 3]))
>   2.0
>
> which agree with numpy:
>
>>> np.median(1)
>   1.0
>>> np.median(True)
>   1.0
>>> np.median(np.array(1))
>   1.0
>>> np.median(np.array([1, 2, 3]))
>   2.0
>
> I'm keeping a local copy of the changes I made for my own package. But
> it would be nice (for me) if this was fixed upstream. Are the changes
> above good enough for scipy?
>
> (Another difference from np.median that I noticed is that the default
> axis for np.median is None and for scipy.stats.nanmean it is 0. But
> perhaps it is too late to change that.)
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```

More information about the SciPy-User mailing list