[Numpy-discussion] Error using rec.fromrecords with specific dtype
Sameer DCosta
sameerslists@gmail....
Thu Aug 7 14:44:37 CDT 2008
Hi,
I am unable to specify a dtype when using numpy.rec.fromrecords. This
problem only occurs when one of the dtypes is an object. (Please see
below)
Thanks in advance for your help.
Sameer
In [7]: import datetime, numpy as np
In [8]: print np.__version__
1.2.0.dev5253
In [9]: dtype=np.dtype([('spam', '|O4'),('ham', '<i4')] )
In [10]: data = [ (datetime.date(2001,1,1), 2),
(datetime.date(2001,1,2), 4), (datetime.date(2001,1,3), 6) ]
In [11]: n = np.rec.fromrecords(data, dtype=dtype)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/titan/sameer/<ipython console>
/home/titan/sameer/local/mins/lib/python2.4/site-packages/numpy/core/records.pyc
in fromrecords(recList, dtype, shape, formats, names, titles, aligned,
byteorder)
469 res = retval.view(recarray)
470
--> 471 res.dtype = sb.dtype((record, res.dtype))
472 return res
473
/home/titan/sameer/local/mins/lib/python2.4/site-packages/numpy/core/records.pyc
in __setattr__(self, attr, val)
287 if attr not in fielddict:
288 exctype, value = sys.exc_info()[:2]
--> 289 raise exctype, value
290 else:
291 fielddict =
ndarray.__getattribute__(self,'dtype').fields or {}
TypeError: Cannot change data-type for object array.
In [12]: names = "spam,ham"
In [13]: n = np.rec.fromrecords(data, names=names)
In [14]: print n
[(datetime.date(2001, 1, 1), 2) (datetime.date(2001, 1, 2), 4)
(datetime.date(2001, 1, 3), 6)]
More information about the Numpy-discussion
mailing list