[NumPy-Tickets] [NumPy] #1717: npma.sum(a_ma, axis=1) fails when no masked values are found

NumPy Trac numpy-tickets@scipy....
Mon Jan 17 23:12:59 CST 2011


#1717: npma.sum(a_ma,axis=1) fails when no masked values are found
------------------------------------+---------------------------------------
 Reporter:  markhmoulton@…          |       Owner:  pierregm
     Type:  defect                  |      Status:  new     
 Priority:  normal                  |   Milestone:  2.0.0   
Component:  numpy.ma                |     Version:  1.4.0   
 Keywords:  mask, sum, axis=1       |  
------------------------------------+---------------------------------------
 I am using the Enthought distribution, Numpy version 1.4.0.  Maybe this
 bug has been fixed already, but I thought it serious enough to mention
 anyway just in case.

 As the snippet shows, the column of sums of a masked array (axis = 1) is
 valid when there is at least one masked value, but triggers an error when
 no masked values are present.  This behavior does not occur when axis = 0
 or when the average is used instead.

 This causes Numpy to return an error in a situation that arises routinely,
 but not uniformly.

 FILE
     /Library/Frameworks/EPD64.framework/Versions/6.2/lib/python2.6/site-
 packages/numpy/version.py

 DATA
     release = True
     short_version = '1.4.0'
     version = '1.4.0'

 >>> a = npr.randint(0,5,(4,3))
 >>> a
 array([[0, 3, 1],
        [1, 0, 2],
        [1, 4, 4],
        [3, 0, 3]])

 # Works when 0 is the value to be masked
 >>> a0_ma = npma.masked_values(a,0)
 >>> np.sum(a0_ma,axis=1)
 masked_array(data = [4 3 9 6],
              mask = [False False False False],
        fill_value = 999999)

 # Fails when 9 is the value (not present) to be masked
 >>> a9_ma = npma.masked_values(a,9)
 >>> npma.sum(a9_ma,axis=1)

 Traceback (most recent call last):
   File "<pyshell#27>", line 1, in <module>
     np.sum(a9_ma,axis=1)
   File "/Library/Frameworks/EPD64.framework/Versions/6.2/lib/python2.6
 /site-packages/numpy/core/fromnumeric.py", line 1390, in sum
     return sum(axis, dtype, out)
   File "/Library/Frameworks/EPD64.framework/Versions/6.2/lib/python2.6
 /site-packages/numpy/ma/core.py", line 4357, in sum
     newmask = _mask.all(axis=axis)
 ValueError: axis(=1) out of bounds

 # The problem does not appear when we sum along the row axis
 >>> npma.sum(a9_ma,axis=0)
 masked_array(data = [ 5  7 10],
              mask = False,
        fill_value = 999999)

 # Nor does it appear when we take the average
 >>> npma.average(a9_ma,axis=1)
 array([ 1.33333333,  1.        ,  3.        ,  2.        ])

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1717>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list