You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Overwrite of alpha values when casting a string slice to int (Trac #2005) #2598
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.
Activity
charris commentedon Feb 21, 2014
Still broken in 1.9-devel
seberg commentedon Jul 3, 2017
Strange bug, but fixed in 1.12, so closing.