[Numpy-discussion] numpy.array() of mixed integers and strings can truncate data

Pierre Haessig pierre.haessig at crans.org
Thu Dec 1 09:47:47 EST 2011


Le 01/12/2011 14:52, Thouis (Ray) Jones a écrit :
> Is this expected behavior?
> 
>>>> np.array([-345,4,2,'ABC'])
> array(['-34', '4', '2', 'ABC'], dtype='|S3')
> 
> 
With my numpy 1.5.1, I got indeed a different result:

In [1]: np.array([-345,4,2,'ABC'])
Out[1]:
array(['-345', '4', '2', 'ABC'],
      dtype='|S8')

The type casting is a bit different, and actually may better match what
you expect, but still a casting is required
(i.e. you cannot have a "numpy.array() of mixed integers and strings"
because numpy arrays only store *homogenous* sets of data)

Now one question remains for me : why use a numpy array to store a few
strings, and not just a regular Python list ?

Best,
Pierre



More information about the NumPy-Discussion mailing list