[Numpy-discussion] Status of NumPy and Python 3.3

Ondřej Čertík ondrej.certik@gmail....
Sat Jul 28 10:12:15 CDT 2012


On Sat, Jul 28, 2012 at 8:04 AM, Ondřej Čertík <ondrej.certik@gmail.com> wrote:
> On Sat, Jul 28, 2012 at 7:58 AM, Ondřej Čertík <ondrej.certik@gmail.com> wrote:
> [...]
>> Here is the code in defchararray.py:
>>
>>
>> 1911            if not _globalvar and self.dtype.char not in 'SUbc':
>> 1912                raise ValueError("Can only create a chararray from
>> string data.")
>> 1913
>> 1914        def __getitem__(self, obj):
>> 1915            val = ndarray.__getitem__(self, obj)
>> 1916 ->         if issubclass(val.dtype.type, character) and not _len(val) == 0:
>> 1917                temp = val.rstrip()
>> 1918                if _len(temp) == 0:
>> 1919                    val = ''
>> 1920                else:
>> 1921                    val = temp
>>
>>
>> and here is some debugging info:
>>
>
> Python 3.3:
>
>>
>> (Pdb) p self
>> (Pdb) p obj
>> (0, 0)
>> (Pdb) p val
>> 'abc'
>> (Pdb) p type(val)
>> <class 'str'>
>
> Python 3.2:
>
> (Pdb) p self
> chararray([['abc', '123'],
>        ['789', 'xyz']],
>       dtype='<U3')
> (Pdb) p obj
> (0, 0)
> (Pdb) p val
> 'abc'
> (Pdb) p type(val)
> <class 'numpy.str_'>
>
>
> So I think there might be some conversion issues int the chararray,
> that instead of using numpy.str_, it uses Python's str.
> Weird.

Ok, found this minimal example of the problem. Python 3.3:

>>> from numpy import array
>>> a = array(["123", "abc"])
>>> a
array(['123', 'abc'],
      dtype='<U3')
>>> a[0]
'123'
>>> type(a[0])
<class 'str'>


Python 3.2:

>>> from numpy import array
>>> a = array(["123", "abc"])
>>> a
array(['123', 'abc'],
      dtype='<U3')
>>> a[0]
'123'
>>> type(a[0])
<class 'numpy.str_'>

So at some point, the strings get converted to numpy strings in 3.2,
but not in 3.3.

Ondrej


More information about the NumPy-Discussion mailing list