[Numpy-discussion] recarray

Francesc Altet faltet at carabos.com
Mon Sep 18 11:40:40 EDT 2006


El dl 18 de 09 del 2006 a les 17:10 +0200, en/na Lionel Roubeyrie va
escriure:
> Le lundi 18 septembre 2006 12:17, Francesc Altet a écrit :
> > You have two problems here. The first is that you shouldn't have missign
> > entries, or conversion from empty strings to ints (or whatever) will
> >
> > fail:
> > >>> int('')
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > ValueError: invalid literal for int():
> >
> > Second, you can't feed a string of literals directly into the rec.array
> > constructor (it is not as intelligent to digest this yet). You can
> >
> > achieve what you want by first massaging the data a bit:
> > >>> ra=numpy.rec.array(datas[1:])
> >
> > numpy.rec.fromarrays([ra['f1'],ra['f2'],ra['f3'],ra['f4'],ra['f5']],formats
> >='a10,i2,a1,i2,a1') recarray([('05/01/2006', 33, 'A', 0, 'N'),
> > ('06/01/2006', 41, 'A', 30, 'A'),
> >        ('07/01/2006', 20, 'A', 16, 'A'), ('08/01/2006', 16, 'A', 13,
> > 'A')],
> >       dtype=[('f1', '|S10'), ('f2', '<i2'), ('f3', '|S1'), ('f4',
> > '<i2'), ('f5', '|S1')])
> >
> > or, a bit more easier,
> >
> > >>> ca=numpy.array(datas[1:])
> > >>> numpy.rec.fromarrays(ca.transpose(),formats='a10,i2,a1,i2,a1')
> >
> > recarray([('05/01/2006', 33, 'A', 0, 'N'), ('06/01/2006', 41, 'A', 30,
> > 'A'),
> >        ('07/01/2006', 20, 'A', 16, 'A'), ('08/01/2006', 16, 'A', 13,
> > 'A')],
> >       dtype=[('f1', '|S10'), ('f2', '<i2'), ('f3', '|S1'), ('f4',
> > '<i2'), ('f5', '|S1')])
> >
> >
> > Cheers,
> 
> Hi,
> thanks for your help, but I don't understand why is not working here:
> lionel[ETD-2006-01__PM2.5_DALTON]624>datas
>                          Sortie[624]:
> [['05/01/2006', '33', 'A', '10', 'N'],
>  ['06/01/2006', '41', 'A', '30', 'A'],
>  ['07/01/2006', '20', 'A', '16', 'A']]
> 
> lionel[ETD-2006-01__PM2.5_DALTON]625>ra=rec.array(datas)
> 
> lionel[ETD-2006-01__PM2.5_DALTON]626>ra
>                          Sortie[626]:
> recarray([('05/01/2006', '33', 'A', '10', 'N'),
>        ('06/01/2006', '41', 'A', '30', 'A'),
>        ('07/01/2006', '20', 'A', '16', 'A')],
>       dtype=[('f1', '|S10'), ('f2', '|S2'), ('f3', '|S1'), ('f4', '|S2'), 
> ('f5', '|S1')])
> 
> lionel[ETD-2006-01__PM2.5_DALTON]627>rec.fromarrays( [ra['f1'], ra['f2'], 
> ra['f4']], formats='a10,i2,i2')
> ---------------------------------------------------------------------------
> exceptions.TypeError                                 Traceback (most recent 
> call last)

Mmm, this works for me:

>>> datas
[['05/01/2006', '33', 'A', '0', 'N'], ['06/01/2006', '41', 'A', '30',
'A'], ['07/01/2006', '20', 'A', '16', 'A'], ['08/01/2006', '16', 'A',
'13', 'A']]
>>> ra=rec.array(datas)
>>> rec.fromarrays([ra['f1'],ra['f2'],ra['f4']], formats='a10,i2,i2')
recarray([('05/01/2006', 33, 0), ('06/01/2006', 41, 30),
       ('07/01/2006', 20, 16), ('08/01/2006', 16, 13)],
      dtype=[('f1', '|S10'), ('f2', '<i2'), ('f3', '<i2')])

I'm running NumPy 1.0b5. Please, check that you are using a recent
version of it.

Cheers,

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






More information about the NumPy-Discussion mailing list