[Numpy-discussion] inconsistent behaviour of mean, average and median

Stefan van der Walt stefan at sun.ac.za
Fri Jan 26 02:03:49 EST 2007


Hi,

I noticed the following behaviour for empty lists:

In [4]: N.median([])
---------------------------------------------------------------------------
exceptions.IndexError                                Traceback (most recent call last)

/home/stefan/<ipython console> 

/home/stefan/lib/python2.4/site-packages/numpy/lib/function_base.py in median(m)
   1081         return sorted[index]
   1082     else:
-> 1083         return (sorted[index-1]+sorted[index])/2.0
   1084 
   1085 def trapz(y, x=None, dx=1.0, axis=-1):

IndexError: index out of bounds

In [5]: N.mean([])
Out[5]: nan

In [6]: N.average([])
---------------------------------------------------------------------------
exceptions.ZeroDivisionError                         Traceback (most recent call last)

/home/stefan/<ipython console> 

/home/stefan/lib/python2.4/site-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
    294     if not isinstance(d, ndarray):
    295         if d == 0.0:
--> 296             raise ZeroDivisionError, 'zero denominator in average()'
    297     if returned:
    298         return n/d, d

ZeroDivisionError: zero denominator in average()


Which is the ideal response -- NaN or an exception, and if an exception,
of which kind?

Cheers
Stéfan



More information about the NumPy-Discussion mailing list