dtype from |S10 to object in array?
![](https://secure.gravatar.com/avatar/2f7019432859c5ed11b658c0384e8aa6.jpg?s=120&d=mm&r=g)
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'], dtype='|S10') x['f2'] = y x #array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')]) y = y.astype('object') y 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 Rgs, KC
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Wed, Sep 22, 2010 at 2:04 AM, keekychen.shared <keekychen.shared@gmail.com> wrote:
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'], dtype='|S10')
x['f2'] = y x #array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
y = y.astype('object') y 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.
I *guess* that you can do this by changing only the type of the string column to object dt = np.dtype([('f0', '<i4'), ('f1', '<f4'), ('f2', object)])
x = np.zeros((2,),dtype=('i4,f4,O')) x[:] = [(1,2.,'Hello'),(2,3.,"World")] x array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|O4')]) x['f2'] = 'hellooooooooooooooooo' x array([(1, 2.0, 'hellooooooooooooooooo'), (2, 3.0, 'hellooooooooooooooooo')], dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|O4')])
Josef
Thanks for any hints
Rgs,
KC
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/40489da22d2dc0cc12596420bb810463.jpg?s=120&d=mm&r=g)
Assignment will never change the data-type of an already allocated array. So, x['f2'] = y will try to convert the array to 'S10'. On my system I get: array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')]) which is what I would expect. If you want to change the data, then create a new data-type: x2 = np.zeros((2,),dtype=('i4,f4,O')) x2[:] = [(1,2.,'Hello'), (2,3.,'World')] -Travis On Sep 22, 2010, at 2:04 AM, keekychen.shared wrote:
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'], dtype='|S10')
x['f2'] = y x #array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
y = y.astype('object') y 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
Rgs,
KC
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
--- Travis Oliphant Enthought, Inc. oliphant@enthought.com 1-512-536-1057 http://www.enthought.com
participants (3)
-
josef.pktd@gmail.com
-
keekychen.shared
-
Travis Oliphant