[Numpy-discussion] Setting scalar values from strings, what to do?

Charles R Harris charlesr.harris@gmail....
Sun Apr 13 01:12:15 CDT 2008


Reposting the comment I left on ticket #736 because I think we need to
decide how conversion of strings to scalars should work.

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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080413/7c23266f/attachment.html 


More information about the Numpy-discussion mailing list