[Numpy-discussion] Astype and strings

Eli Bressert ebressert@cfa.harvard....
Mon Oct 26 09:54:37 CDT 2009


Hi Everyone,

Is Numpy supposed to behave this like this when converting an array of
numbers to an array of strings with astype?

print(arange(20).astype(np.str))
['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '1' '1' '1' '1' '1' '1' '1'
'1' '1' '1']

When I do the following it works fine,

print(arange(20).astype('|S2'))
['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15'
'16' '17' '18' '19']

I would have thought that astype would be more intelligent with
strings rather than just resorting to the first character for each
element. Is this a bug or or is it how astype works?

Thanks,

Eli


More information about the NumPy-Discussion mailing list