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
On Oct 26, 2009, at 9:54 AM, Eli Bressert wrote:
Hi Everyone,
Is Numpy supposed to behave this like this when converting an array of numbers to an array of strings with astype?
In general you have to tell NumPy how big the string should be (i.e. np.str is generic).
There are a few places where NumPy will look at the data you have in order to guess a size, but as you've seen astype is not one of those places.
I think astype could be fixed (by putting a special-case check in the current code for conversion to an unspecified-length string), but that has not been implemented.
Please file a feature enhancement issue on the Trac so we don't lose sight of this.
-Travis