I have a numpy record array and I want to pretty print a single
element. I was trying to loop over the names in the element dtype and
use getattr to access the field value, but I got fouled up because
getattr is trying to access the dtype attribute of one of the python
objects (datetime.date) that I am storing in the record array. The
problem field in the example below is ('fiscaldate', '|O4') which is a
python datetime.date object.
I subsequently discovered that I can simply use tr[name] rather than
getattr(tr, name),but wanted to post this in case it is a bug.
In other news, would it be possible to add a pprint method to record
array elements that did something like the pprint_element below? It
would be nice to be able to do tr.pprint() and get nice output which
shows key/value pairs.
Thanks,
JDH
=======================================================
# the buggy code
In [33]: type(thisr)
Out[33]: <class 'numpy.core.records.recarray'>
In [34]: tr = thisr[14]
In [35]: tr
Out[35]: ('CDE', 'Y2002', datetime.date(2002, 12, 31),
datetime.date(2002, 3, 18), datetime.date(2003, 3, 18),
datetime.date(2004, 3, 18), datetime.date(2005, 3, 18), 'CDE', 'A',
767.0, 126.95, 85.944000000000003, -81.207999999999998, -8.484,
4.9589999999999996, -28.071999999999999, 33.030000000000001,
415.96499999999997, 184.334)
In [36]: tr.dtype
Out[36]: dtype([('id', '|S10'), ('period', '|S5'), ('fiscaldate',
'|O4'), ('datepy1', '|O4'), ('reportdate', '|O4'), ('dateny1', '|O4'),
('dateny2', '|O4'), ('ticker', '|S6'), ('exchange', '|S1'),
('employees', '<f8'), ('marketcap', '<f8'), ('sales', '<f8'),
('income', '<f8'), ('cashflowops', '<f8'), ('returnunhedged', '<f8'),
('returnq', '<f8'), ('returnhedgedprior', '<f8'),
('returnhedgednext1', '<f8'), ('returnhedgednext2', '<f8')])
In [37]: for name in tr.dtype.names:
print name, getattr(tr, name)
....:
id CDE
period Y2002
fiscaldate------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 2, in ?
File
"/home/titan/johnh/dev/lib/python2.4/site-packages/numpy/core/records.py",
line 138, in __getattribute__
if obj.dtype.fields:
AttributeError: 'datetime.date' object has no attribute 'dtype'
In [39]: numpy.__version__
Out[39]: '1.0.3.dev3728'
======================================================
# the good code (the colons are aligned in the output in an ASCII terminal)
def pprint_element(tr):
names = tr.dtype.names
maxlen = max([len(name) for name in names])
rows = []
fmt = '%% %ds: %%s'%maxlen
for name in names:
rows.append(fmt%(name, tr[name]))
return '\n'.join(rows)
In [49]: print pprint_element(tr)
id: CDE
period: Y2002
fiscaldate: 2002-12-31
datepy1: 2002-03-18
reportdate: 2003-03-18
dateny1: 2004-03-18
dateny2: 2005-03-18
ticker: CDE
exchange: A
employees: 767.0
marketcap: 126.95
sales: 85.944
income: -81.208
cashflowops: -8.484
returnunhedged: 4.959
returnq: -28.072
returnhedgedprior: 33.03
returnhedgednext1: 415.965
returnhedgednext2: 184.334
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion