[Numpy-discussion] Bug in MaskedArray min/max functions

Philip Cooper philip.cooper@openvest....
Wed Apr 28 11:33:16 CDT 2010


if a masked array is created and has no masked values it can have a mask of just "False" scalar false that is.

This causes an error when getting the max or min on axis=1
I don't have a fix but do offer a workaround.
If you reset the mask to the "expanded"mask all works ok (but it's a bug that pops up all over my code)

Below "a" and "m1" work but "m5" only works on axis = 0.  anyway you can look at:   (see http://www.openvest.com/trac/wiki/MaskedArrayMinMax if the formatting doesn't survive the mail posting process)

#################################
>>> import numpy as np
>>> a = np.array([np.arange(5)])
>>> a
array([[0, 1, 2, 3, 4]])
>>> m1 = np.ma.masked_values(a,1)
>>> m5 = np.ma.masked_values(a,5)
>>> m1
masked_array(data =
 [[0 -- 2 3 4]],
             mask =
 [[False  True False False False]],
       fill_value = 1)

>>> m5
masked_array(data =
 [[0 1 2 3 4]],
             mask =
 False,
       fill_value = 5)

>>> a.min(axis=0)
array([0, 1, 2, 3, 4])
>>> m5.min(axis=0)
masked_array(data = [0 1 2 3 4],
             mask = False,
       fill_value = 999999)

>>> m1.min(axis=0)
masked_array(data = [0 -- 2 3 4],
             mask = [False  True False False False],
       fill_value = 999999)

>>> a.min(axis=1)
array([0])
>>> m1.min(axis=1)
masked_array(data = [0],
             mask = [False],
       fill_value = 999999)
>>> m5.min(axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/numpy/ma/core.py", line 5020, in min
    newmask = _mask.all(axis=axis)
ValueError: axis(=1) out of bounds
### workaround
>>> m5.mask = np.ma.getmaskarray(m5)
>>> m1.min(axis=1)
masked_array(data = [0],
             mask = [False],
       fill_value = 999999)
-- 
Philip J. Cooper (CFA)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100428/124d8fec/attachment.html 


More information about the NumPy-Discussion mailing list