[NumPy-Tickets] [NumPy] #2005: Overwrite of alpha values when casting a string slice to int

NumPy Trac numpy-tickets@scipy....
Fri Dec 23 01:06:25 CST 2011


#2005: Overwrite of alpha values when casting a string slice to int
-----------------------------------------------------+----------------------
 Reporter:  markhmoulton@…                           |       Owner:  somebody   
     Type:  defect                                   |      Status:  new        
 Priority:  normal                                   |   Milestone:  Unscheduled
Component:  Other                                    |     Version:  1.6.0      
 Keywords:  type casting, string, alpha, int, slice  |  
-----------------------------------------------------+----------------------
 When trying to cast a slice of string values to int, where one of the cell
 values is alpha, the alpha value is mistakenly converted into an integer.
 Instead, it should return a ValueError exception.

 {{{
 >>> B = np.zeros((5,3)).astype('S10')
 >>> B[:,0] = range(5)
 >>> B[0,0] = 'StudentID'
 >>> B
 array([['StudentID', '0.0', '0.0'],
        ['1', '0.0', '0.0'],
        ['2', '0.0', '0.0'],
        ['3', '0.0', '0.0'],
        ['4', '0.0', '0.0']],
       dtype='|S10')

 # Casting the whole array to int throws an error, as it should.
 >>> B.astype(int)

 Traceback (most recent call last):
   File "<pyshell#152>", line 1, in <module>
     B.astype(int)
 ValueError: invalid literal for int() with base 10: 'StudentID'

 # However, casting a slice of the same array to int actually changes
 'StudentID' into an integer (11 in this case).
 >>> B[:,0].astype(int)
 array([11,  1,  2,  3,  4])
 >>>
 }}}

 This behavior makes it hard to catch important ValueError exceptions,
 besides mangling my data.

 My versions are Python 2.7.2 and Numpy 1.6.0, as distributed
 by Enthought (EPD 7.1-1 (64-bit)), on a Mac Pro.

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


More information about the NumPy-Tickets mailing list