From: Lionel R. <lro...@li...> - 2006-09-18 15:10:56
|
Le lundi 18 septembre 2006 12:17, Francesc Altet a =C3=A9crit=C2=A0: > 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=3Dnumpy.rec.array(datas[1:]) > > numpy.rec.fromarrays([ra['f1'],ra['f2'],ra['f3'],ra['f4'],ra['f5']],forma= ts >=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, 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=3Drec.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=3D[('f1', '|S10'), ('f2', '|S2'), ('f3', '|S1'), ('f4', '|S2'),= =20 ('f5', '|S1')]) lionel[ETD-2006-01__PM2.5_DALTON]627>rec.fromarrays( [ra['f1'], ra['f2'],=20 ra['f4']], formats=3D'a10,i2,i2') =2D------------------------------------------------------------------------= =2D- exceptions.TypeError Traceback (most recent= =20 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=20 fromarrays(arrayList, formats, names, titles, shape, aligned) 235 # populate the record array (makes a copy) 236 for i in range(len(arrayList)): =2D-> 237 _array[_names[i]] =3D arrayList[i] 238 239 return _array TypeError: array cannot be safely cast to required type =2D-=20 Lionel Roubeyrie - lro...@li... LIMAIR http://www.limair.asso.fr |