Using numpy.median for a masked array

I am a bit confused about the output of numpy.median in the case of masked arrays. Here is a simple example (assuming numpy is imported - I have version 1.6.2):

>>> a = [3.0, 4.0, 5.0, 6.0, numpy.nan] >>> am = numpy.ma.masked_array(a, [numpy.isnan(x) for x in a]) 

I would like to be able to use a masked array to ignore the nan values ​​in the array when calculating the median. This works for averages using the numpy.mean or mean() method of the masked array:

 >>> numpy.mean(a) nan >>> numpy.mean(am) 4.5 >>> am.mean() 4.5 

However, for the median, I get:

 >>> numpy.median(am) 5.0 

but I would expect something more than this result:

 >>> numpy.median([x for x in a if not numpy.isnan(x)]) 4.5 

and unfortunately masked_array does not have a median method.

+7
source share
1 answer

Use np.ma.median on MaskedArray .

[Explanation: If I remember correctly, np.median does not support subclasses, so it does not work correctly on np.ma.MaskedArray .]

+9
source

All Articles