[Numpy-discussion] Casting RecArray fields?

Francesc Altet faltet at carabos.com
Tue Jan 31 02:46:01 EST 2006


A Dimarts 31 Gener 2006 10:46, dHering va escriure:
> Hi all,
>
> I need to cast fields of type 'Bool' to type 'Int32'.
> I'm not sure how to do this.
>
> Thank you,
> Dieter
>
>
> import numarray.records as rec
>
> b = rec.array([[23, 0],[45, 1]], names=' integer, boolean',formats='Int32,
> Bool')
>
> print b.field(1).type()
> #  following line is not permissable
> #  b.field(1) = b.field(1).astype('Int32')
> b.field(1)[:] = b.field(1).astype('Int32')
> print b.field(1).type()

You can't do this because this implies a change on the column type
definition (and also in the type size) of the recarray.

One way that works for me, but that implies a complete data copy, is:

In [15]: c = rec.array([b.field(0),b.field(1)], formats='Int32,Int32')

In [16]: c.field(1)
Out[16]: array([0, 1])

HTH,

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"





More information about the NumPy-Discussion mailing list