[Numpy-discussion] recarray

Francesc Altet faltet at carabos.com
Fri Sep 15 09:05:55 CDT 2006

A Divendres 15 Setembre 2006 15:57, Robert Kern va escriure:
> Lionel Roubeyrie wrote:
> > Hi all,
> > I try to use recarray with rec.fromrecords on time-series, datas come
> > from a file where they are stored in csv format, with after each data
> > colum there is one column meanning the state of the data, and the first
> > column is for dates. Then, is it possible to directly transform column of
> > strings to a integer one (or datetime one), and to remove a not used
> > column?
> When I import CSV files into record arrays, I usually read in all of the
> data and transpose the list of rows to get a list of columns. Then I can
> remove columns and transform them _en masse_, usually with map().

Another possibility is to play with columns directly from the initial 
recarray. The next is an example:

In [101]: ra=numpy.rec.array("1"*36, dtype="a4,i4,f4", shape=3)
In [102]: ra
recarray([('1111', 825307441, 2.5784852031307537e-09),
       ('1111', 825307441, 2.5784852031307537e-09),
       ('1111', 825307441, 2.5784852031307537e-09)],
      dtype=[('f0', '|S4'), ('f1', '<i4'), ('f2', '<f4')])
In [103]: rb=numpy.rec.fromarrays([numpy.array(ra['f0'], 'i4'),ra['f2']], 
In [104]: rb
recarray([(1111, 2.5784852031307537e-09), (1111, 2.5784852031307537e-09),
       (1111, 2.5784852031307537e-09)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])

where ra is the original recarray and rb is a derived one where its first 
column is the original from ra, but converted to integers ('i4'), and the 
second it's the third column from ra (so the second column from ra has been 
stripped out from rb).


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

More information about the Numpy-discussion mailing list