Skip to content

Overwrite of alpha values when casting a string slice to int (Trac #2005) #2598

Closed

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/2005 on 2011-12-23 by trac user markhmoulton@..., assigned to unknown.

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

  1. charris commented on Feb 21, 2014

    @charris
    Member

    Still broken in 1.9-devel

  2. seberg commented on Jul 3, 2017

    @seberg
    Member

    Strange bug, but fixed in 1.12, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions