[Numpy-discussion] Error creating an array
Travis E. Oliphant
Tue Aug 5 15:04:43 CDT 2008
Sameer DCosta wrote:
> On Tue, Aug 5, 2008 at 2:35 PM, Travis E. Oliphant
> <firstname.lastname@example.org> 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?
Yeah.. complexity of the array command (it process a lot of cases).
There may be a way to make it work differently for record arrays, but
nobody has done that.
> 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
You can always use b['spam'] to look up the field. The record is a
new data-type sub-classed from void that allows attribute look up for
field access. Perhaps we should automatically convert the dtype at the
same time that we convert type type. But, I'm not really sure.
Basically, to get the behavior you want, the dtype of the array needs to
be changed also to that of record instead of the void type with fields
defined. This is fundamentally different than changing just the
type-object of the array itself to the record-array.
You can get the behavior you want with:
b = b.view(dtype=(np.record, b.dtype), type=np.recarray).
This manually changes not only the type of the array to a recarray but
also the data-type of the elements in the array to be "records" (with
attribute lookup of fields) instead of the default "void" type with
fields accesible through mapping semantics.
More information about the Numpy-discussion