[Numpy-discussion] making a character array from numeric array

Michael Sorich michael.sorich at gmail.com
Mon Mar 27 15:37:12 CST 2006


 <Numpy-discussion at lists.sourceforge.net>Hi,

I am trying to convert an int or float ndarray into a string ndarray. Using
astype this is possible, but only if you explicity set the number of
characters (see eg below). It would be nice if just setting type='S' (or
N.str_) would automatically make an a character array of sufficient size to
hold the string representation. Is there a way to do this?

thanks,

michael

import numpy as N
N.version.version
'0.9.6'
N.arange(20).astype(N.str_)
array([, , , , , , , , , , , , , , , , , , , ],
      dtype='|S0')
N.arange(20).astype('S')
array([, , , , , , , , , , , , , , , , , , , ],
      dtype='|S0')
N.arange(20).astype('S1')
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      dtype='|S1')
N.arange(20).astype('S2')
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19],
      dtype='|S2')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060327/98aa41e1/attachment.html 


More information about the Numpy-discussion mailing list