From: Francesc A. <fa...@ca...> - 2006-09-18 15:40:53
|
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 =C3=A9crit : > > You have two problems here. The first is that you shouldn't have missig= n > > 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=3Dnumpy.rec.array(datas[1:]) > > > > numpy.rec.fromarrays([ra['f1'],ra['f2'],ra['f3'],ra['f4'],ra['f5']],for= mats > >=3D'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=3D[('f1', '|S10'), ('f2', '<i2'), ('f3', '|S1'), ('f4', > > '<i2'), ('f5', '|S1')]) > > > > or, a bit more easier, > > > > >>> ca=3Dnumpy.array(datas[1:]) > > >>> numpy.rec.fromarrays(ca.transpose(),formats=3D'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=3D[('f1', '|S10'), ('f2', '<i2'), ('f3', '|S1'), ('f4', > > '<i2'), ('f5', '|S1')]) > > > > > > Cheers, >=20 > 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']] >=20 > lionel[ETD-2006-01__PM2.5_DALTON]625>ra=3Drec.array(datas) >=20 > 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=3D[('f1', '|S10'), ('f2', '|S2'), ('f3', '|S1'), ('f4', '|S2'= ),=20 > ('f5', '|S1')]) >=20 > lionel[ETD-2006-01__PM2.5_DALTON]627>rec.fromarrays( [ra['f1'], ra['f2'],= =20 > ra['f4']], formats=3D'a10,i2,i2') > -------------------------------------------------------------------------= -- > exceptions.TypeError Traceback (most rece= nt=20 > 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=3Drec.array(datas) >>> rec.fromarrays([ra['f1'],ra['f2'],ra['f4']], formats=3D'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=3D[('f1', '|S10'), ('f2', '<i2'), ('f3', '<i2')]) I'm running NumPy 1.0b5. Please, check that you are using a recent version of it. Cheers, --=20 >0,0< Francesc Altet http://www.carabos.com/ V V C=C3=A1rabos Coop. V. Enjoy Data "-" |