[Numpy-discussion] recarray

Lionel Roubeyrie lroubeyrie at limair.asso.fr
Mon Sep 18 11:10:10 EDT 2006


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)

/home/lionel/Etudes_Techniques/ETD-2006-01__PM2.5_DALTON/<ipython console>

/usr/lib/python2.4/site-packages/numpy/core/records.py in 
fromarrays(arrayList, formats, names, titles, shape, aligned)
    235     # populate the record array (makes a copy)
    236     for i in range(len(arrayList)):
--> 237         _array[_names[i]] = arrayList[i]
    238
    239     return _array

TypeError: array cannot be safely cast to required type


-- 
Lionel Roubeyrie - lroubeyrie at limair.asso.fr
LIMAIR
http://www.limair.asso.fr




More information about the NumPy-Discussion mailing list