[Numpy-discussion] Error creating an array

Sameer DCosta sameerslists@gmail....
Tue Aug 5 14:46:34 CDT 2008


On Tue, Aug 5, 2008 at 2:35 PM, Travis E. Oliphant
<oliphant@enthought.com> wrote:

> You must uses tuples for the individual "records" when constructing
> arrays with the "array" command.

Thanks Travis. Is there a reason why numpy insists on tuples?
Anyway, moving on, this brings me to the real problem I wanted to
post. I can do an attribute lookup on an element of a recarray which
has been created by fromrecords, however I cannot do attribute lookups
on ndarrays that have been viewed as recarrays. Any ideas how to
proceed.

In [469]: import numpy as np

In [470]: print np.__version__
1.2.0.dev5243

In [471]: dtype=np.dtype([('spam', '<i4'),('ham', '<i4')] )

In [472]: data = [ (1,2), (3,4), (5,6) ]

In [473]: n = np.rec.fromrecords(data, dtype=dtype)

In [474]: print "n[0].spam=", n[0].spam
n[0].spam= 1

In [475]: b = np.array( data, dtype=dtype)

In [476]: b = b.view(np.rec.recarray)

In [477]: print "b[0].spam=", b[0].spam
b[0].spam=---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/titan/sameer/tmp/test.py
----> 1
      2
      3
      4
      5

AttributeError: 'numpy.void' object has no attribute 'spam'

In [478]: type(n[0])
Out[478]: <class 'numpy.core.records.record'>

In [479]: type(b[0])
Out[479]: <type 'numpy.void'>


More information about the Numpy-discussion mailing list