(Newbie alert.) I'm having trouble making a nested record array. I'm trying to work from the following example on the scipy.org examples page:
mydescriptor = dtype([('x', 'f4'),('y', 'f4'), # nested recarray ... ('nested', [('i', 'i2'),('j','i2')])]) myarr = array([(1.0, 2.0, (1,2))], dtype=mydescriptor)
... but this isn't really a nested recarray, since you can't refer to fields 'x', 'y', or 'nested' as attributes:
myarr.x AttributeError: 'numpy.ndarray' object has no attribute 'x'
You have to use the more cumbersome bracket notation:
myarr['x'] array([ 1.], dtype=float32)
When I try modifying the above example by simply replacing the 'array' constructor with 'recarray', I get the following error, which I haven't really grokked yet:
myrecarr = N.recarray([(1.0, 2.0, (1,2))], dtype=dt)
--------------------------------------------------------------------------- exceptions.TypeError Traceback (most recent call last) /home/mkg/<ipython console> /usr/lib/python2.4/site-packages/numpy/core/records.py in __new__(subtype, shape, dtype, buf, offset, strides, formats, names, titles, byteorder, aligned) 176 177 if buf is None: --> 178 self = sb.ndarray.__new__(subtype, shape, (record, descr)) 179 else: 180 self = sb.ndarray.__new__(subtype, shape, (record, descr), TypeError: an integer is required I'm trying to make a record array that stores two record arrays called "states" and "controls", each of which store two float arrays "x" and "dx". The dtype would be something like: dtype([('states', [('x', 'f4'), ('dx, 'f4')]), ('controls', [('x', 'f4'), ('dx', 'f4')])]) It'd be great if I could address elements as: myarr.states.x[0] as opposed to myarr['states']['x'][0] Any tips would be greatly appreciated. Once I figure this out, I'd be glad to post the solution as an example under the "recarray()" entry in the examples list. -- Matt
Matthew Koichi Grimes wrote:
(Newbie alert.)
I'm having trouble making a nested record array. I'm trying to work from the following example on the scipy.org examples page:
mydescriptor = dtype([('x', 'f4'),('y', 'f4'), # nested recarray ... ('nested', [('i', 'i2'),('j','i2')])]) myarr = array([(1.0, 2.0, (1,2))], dtype=mydescriptor)
... but this isn't really a nested recarray, since you can't refer to fields 'x', 'y', or 'nested' as attributes:
It is nested but, yes, it's not as convenient as attribute access.
When I try modifying the above example by simply replacing the 'array' constructor with 'recarray', I get the following error, which I haven't really grokked yet:
The problem is that recarray is not analagous with array. It is analagous with ndarray. Use N.rec.array instead. -Travis
participants (2)
-
Matthew Koichi Grimes
-
Travis Oliphant