SciPy

scipy.stats.nanmedian

scipy.stats.nanmedian(x, axis=0)[source]

Compute the median along the given axis ignoring nan values.

Parameters :

x : array_like

Input array.

axis : int, optional

Axis along which the median is computed. Default is 0, i.e. the first axis.

Returns :

m : float

The median of x along axis.

See also

nanstd, nanmean

Examples

>>> from scipy import stats
>>> a = np.array([0, 3, 1, 5, 5, np.nan])
>>> stats.nanmedian(a)
array(3.0)
>>> b = np.array([0, 3, 1, 5, 5, np.nan, 5])
>>> stats.nanmedian(b)
array(4.0)

Example with axis:

>>> c = np.arange(30.).reshape(5,6)
>>> idx = np.array([False, False, False, True, False] * 6).reshape(5,6)
>>> c[idx] = np.nan
>>> c
array([[  0.,   1.,   2.,  nan,   4.,   5.],
       [  6.,   7.,  nan,   9.,  10.,  11.],
       [ 12.,  nan,  14.,  15.,  16.,  17.],
       [ nan,  19.,  20.,  21.,  22.,  nan],
       [ 24.,  25.,  26.,  27.,  nan,  29.]])
>>> stats.nanmedian(c, axis=1)
array([  2. ,   9. ,  15. ,  20.5,  26. ])

Previous topic

scipy.stats.nanstd

Next topic

scipy.stats.variation