
I'm trying to convert a character array to a floating point array. I'm using one of the recent svn builds. It is surprising that astype does not do the job. However if I first convert the char array to an array and then use astype everything works fine. Is this a bug?
import numpy as N print N.__version__ # Output = '1.0.5.dev4426' a = N.char.array(['123.45', '234.56']) b = N.array(a).astype(N.float) # This works. print b b = a.astype(N.float) # This does not work and raises an exception
ValueError: Can only create a chararray from string data.
Thanks. Sameer

Sameer,
I can't tell whether it's a bug or a feature, but I can give you some explanation: when you call .astype on your chararray, you call the __array_finalize__ of the chararray, which requires the dtype to be string like. Obviously, that won't work in your case. Transforming the chararray to a regular array of strings bypass this problem. That's what you're doing with the N.array(a) statement in your example.
Two comments, however: * Try to use N.asarray() instead, as you won't copy the data (or use N.array(a,copy=False)) * You can also view your charray as a regular ndarray, and then use the astype method: a.view(N.ndarray).astype(float_)

a does not seem to be an array, so it is not surprising that you need to convert it to an array first.
Matthieu
2007/11/28, Sameer DCosta sameerslists@gmail.com:
I'm trying to convert a character array to a floating point array. I'm using one of the recent svn builds. It is surprising that astype does not do the job. However if I first convert the char array to an array and then use astype everything works fine. Is this a bug?
import numpy as N print N.__version__ # Output = '1.0.5.dev4426' a = N.char.array(['123.45', '234.56']) b = N.array(a).astype(N.float) # This works. print b b = a.astype(N.float) # This does not work and raises an exception
ValueError: Can only create a chararray from string data.
Thanks. Sameer _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

On Wednesday 28 November 2007 13:39:45 Matthieu Brucher wrote:
a does not seem to be an array, so it is not surprising that you need to convert it to an array first.
Well, a *IS* a regular chararray, and therefore a subclass of ndarray (try isinstance). The problem isn't here, it's that the subclass doesn't have its own .astype() method. Instead, we use the standard ndarray.astype, which calls the __array_finalize__ method from the subclass, and this one requires a chararray. Maybe we could implement a simple method: def astype(self, newdtype): self.view(N.ndarray).astype(newdtype) But wouldn't that break something down the line ?

Sameer DCosta wrote:
I'm trying to convert a character array to a floating point array. I'm using one of the recent svn builds. It is surprising that astype does not do the job. However if I first convert the char array to an array and then use astype everything works fine. Is this a bug?
import numpy as N print N.__version__ # Output = '1.0.5.dev4426' a = N.char.array(['123.45', '234.56']) b = N.array(a).astype(N.float) # This works. print b b = a.astype(N.float) # This does not work and raises an exception
ValueError: Can only create a chararray from string data.
The problem is that astype for a chararray will by default try to create a class of chararray. This sub-class only allows string data and so the base-class astype(float) will fail.
The astype method could be over-ridden in order to support automatic conversion to other kinds of arrays, but I lean towards asking "why?" because "explicit is better than implicit" (although it is admittedly arguable which is which in this case...)
-Travis
Thanks. Sameer _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Matthieu Brucher
-
Pierre GM
-
Sameer DCosta
-
Travis E. Oliphant