[Numpy-discussion] median object

Robert Kern robert.kern@gmail....
Tue Jul 7 20:07:02 CDT 2009

On Tue, Jul 7, 2009 at 01:34, Scott Sinclair<scott.sinclair.za@gmail.com> wrote:
>> 2009/7/7 keflavich <keflavich@gmail.com>:
>> Hi, I want to be able to do something like:
>> import numpy
>> x=numpy.array([1,4,4,6,7,3,4,2])
>> x.median()
>> rather than
>> numpy.median(x)
>> so that in a function, I can call
>> x.median()
>> and allow x to be a masked array or a numpy array.
>> Using the ma.median version by default is something of a workaround, because
>> it appears to work on both numpy arrays and masked arrays.  My question,
>> though, is: why is median() not implemented as an attribute of the
>> array/masked array object, while min/max/mean/std are?  Is there a better
>> workaround?
> I think the canonical answer is that it's easier to implement a median
> function using existing array methods than to implement median as a
> method of the array object. I guess no-one has wanted the feature
> badly enough to implement it.

Mostly just history. Many, though certainly not all, single-argument
functions from Numeric became ndarray methods. I believe that median
was added after that event, and there is some resistance to adding yet
another method to ndarray. The implementor of numpy.ma did not feel
that way and found it easier to implement median as a method on masked

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list