I'm a little 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'd like to be able to use the masked array to ignore nan
values in the array when calculating the median. This works for mean using either numpy.mean
or the mean()
method of the masked array:
>>> numpy.mean(a)
nan
>>> numpy.mean(am)
4.5
>>> am.mean()
4.5
However for median I get:
>>> numpy.median(am)
5.0
but I'd expect something more like this result:
>>> numpy.median([x for x in a if not numpy.isnan(x)])
4.5
and unfortunately a masked_array
does not have a median
method.