[Numpy-tickets] [NumPy] #736: Inconsistent integer conversion from strings

NumPy numpy-tickets@scipy....
Sun Apr 13 00:38:42 CDT 2008


#736: Inconsistent integer conversion from strings
--------------------+-------------------------------------------------------
 Reporter:  stefan  |        Owner:  somebody
     Type:  defect  |       Status:  new     
 Priority:  normal  |    Milestone:  1.0.5   
Component:  Other   |      Version:  none    
 Severity:  normal  |   Resolution:          
 Keywords:          |  
--------------------+-------------------------------------------------------
Old description:

> Pauli Virtanen noticed the following behaviour (mentioned as part of #719
> discussion):
>
> {{{
> In [8]: np.array([('123',), ('456',)], dtype=[('num', '<i8')])
> Out[8]:
> array([(123L,), (456L,)],
>       dtype=[('num', '<i8')])
> }}}
>
> vs.
>
> {{{
> In [9]: np.array([('123',), ('456',)], dtype=[('num', '<i4')])
>
> TypeError: expected a readable buffer object
> }}}
>
> I believe this is related to the following inconsistent integer-from-
> string conversions:
>
> {{{
> In [27]: np.int32('12')
> Out[27]: 12
>
> In [28]: np.int64('12')
> Out[28]: array([1, 2], dtype=int64)
> }}}

New description:

 Pauli Virtanen noticed the following behaviour (mentioned as part of #719
 discussion):

 {{{
 In [8]: np.array([('123',), ('456',)], dtype=[('num', '<i8')])
 Out[8]:
 array([(123L,), (456L,)],
       dtype=[('num', '<i8')])
 }}}

 vs.

 {{{
 In [9]: np.array([('123',), ('456',)], dtype=[('num', '<i4')])

 TypeError: expected a readable buffer object
 }}}

 I believe this is related to the following inconsistent integer-from-
 string conversions:

 {{{
 In [27]: np.int32('12')
 Out[27]: 12

 In [28]: np.int64('12')
 Out[28]: array([1, 2], dtype=int64)
 }}}

Comment (by charris):

 There is more to this problem:


 {{{
 In [5]: int8('1')
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /home/charris/<ipython console> in <module>()

 ValueError: setting an array element with a sequence.

 In [6]: int16('1')
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /home/charris/<ipython console> in <module>()

 ValueError: setting an array element with a sequence.

 In [7]: int32('1')
 Out[7]: 1

 In [8]: int64('1')
 Out[8]: array([1], dtype=int64)

 In [9]: uint8('1')
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /home/charris/<ipython console> in <module>()

 ValueError: setting an array element with a sequence.

 In [10]: uint16('1')
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /home/charris/<ipython console> in <module>()

 ValueError: setting an array element with a sequence.

 In [11]: uint32('1')
 Out[11]: array([1], dtype=uint32)

 In [12]: uint64('1')
 Out[12]: array([1], dtype=uint64)


 }}}


 The ones that return arrays go though numpy routines that call
 PyNumber_Long on the string. I suspect int32 is a subtype of the python
 int so that it returns a number. We also have


 {{{
 In [10]: array('11',dtype=uint64)
 Out[10]: array([1, 1], dtype=uint64)

 In [11]: array('11',dtype=int8)
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /home/charris/<ipython console> in <module>()

 ValueError: setting an array element with a sequence.

 In [12]: array('11').astype(int8)
 Out[12]: array(11, dtype=int8)
 }}}



 In other words, we have an inconsistent mess on our hands. I think we need
 to decide what the behavior for all these functions should be, along with
 floats and complex, when presented with strings. And what to do when the
 strings are out of range. These problems arise in the setitem functions in
 arraytypes.inc.src and, I suspect, in the array function itself which
 treats the astype method and dtype keyword differently when strings are
 passed in.

-- 
Ticket URL: <http://scipy.org/scipy/numpy/ticket/736#comment:2>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list