[Numpy-discussion] dtype from |S10 to object in array?

keekychen.shared keekychen.shared@gmail....
Wed Sep 22 01:04:03 CDT 2010

 Dear All,

See below code pls,

import sicpy
import numpy as np

x = np.zeros((2,),dtype=('i4,f4,a10'))
x[:] = [(1,2.,'Hello'),(2,3.,"World")]

y = x['f2']
#array(['Hello', 'World'],

x['f2'] = y
#array([(1, 2.0, 'Hello'), (2, 3.0, 'World')],
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])

y = y.astype('object')
array([Hello, World], dtype=object)

x['f2'] = y
array([(1, 2.0, 'HellWorld'), (2, 3.0, '\x00\x00\x00\x00\x00\x00\x18')],
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])

##here comes the problem: the f2 col type has not been changed and the
data is not I wanted...

here is why I need using this:
suppose I have a datasource, csv, sql based db or what ever look like this:

1, 2.0, 'Hello'
2, 3.0, 'World'
3, 2.0, 'other string'

I want to read them to a numpy array and process it's columns, it has no
problem for processing the float or int type but string.
After reading the manual and found the object dtype may store variable
string then I want to exact the string col into an new array, try to
process it then store back to the numpy "matrix" then store it back to
the data source.

May I know how I can do that? I do not care performance now. 

Thanks for any hints




More information about the NumPy-Discussion mailing list