[Numpy-discussion] recarray field access asymmetry

Benjamin Root ben.root@ou....
Wed Feb 15 02:10:31 CST 2012


On Wednesday, February 15, 2012, Benjamin Root <ben.root@ou.edu> wrote:
>
>
> On Wednesday, February 15, 2012, David Gowers (kampu) <00ai99@gmail.com>
wrote:
>> Hi all,
>>
>> This email is about the difference, given a recarray 'arr',
>>  between
>>
>>
>> A)
>>
>> arr.foo.x[0]
>>
>> and B)
>>
>> arr.foo[0].x
>>
>>
>>
>> Specifically, form A returns the 0-th x value, whereas form B raises
>> AttributeError:
>>
>>
>> Some code demonstrating this:
>>
>>>>> arr = np.zeros((4,), dtype = [('foo',[('x','H'),('y','H')])])
>>>>> a2 = arr.view (np.recarray)
>>>>> a2.foo
>> rec.array([(0, 0), (0, 0), (0, 0), (0, 0)],
>>       dtype=[('x', '<u2'), ('y', '<u2')])
>>
>>>>> a2.foo.x
>> array([0, 0, 0, 0], dtype=uint16)
>>
>>>>> a2.foo.x[0]
>> 0
>>
>>>>> a2.foo[0]
>> (0, 0)
>>>>> a2.foo[0].x
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> AttributeError: 'numpy.void' object has no attribute 'x'
>>
>> (similarly, ``a2[0].foo`` raises an identical AttributeError)
>>
>>
>> This is obstructive, particularly since ``a2.foo[0].x`` is the more
>> logical grouping than ``a2.foo.x[0]`` -- we want the x field of item 0
>> in foo, not the 0th x-value in foo.
>>
>> I see this issue has come up previously...
>> http://mail.scipy.org/pipermail/numpy-discussion/2008-August/036429.html
>>
>> The solution proposed by Travis in that email:
>>
>>  ('arr.view(dtype=(np.record, b.dtype), type=np.recarray)')
>>
>> is ineffective with current versions of NumPy; the result is exactly
>> the same as if you had not done it at all.
>> I've tried various other methods including subclassing recarray and
>> overriding __getitem__ and __getattribute__, with no success.
>>
>> My question is, is there a way to resolve this so that ``a2.foo[0].x``
>> does actually do what you'd expect it to?
>>
>> Thanks,
>> David
>>
>
> Rather than recarrays, I just use structured arrays like so:
>
> A = np.array([(0, 0), (0, 0), (0, 0), (0, 0)],
>                dtype=[('x', '<u2'), ('y', '<u2')])
>
> I can then do:
>
> A['x'][0]
>
> Or
>
> A[0]['x']
>
> This allows me to slice and access the data any way I want.  I have even
been able to use this dictionary idiom to format strings and such.
>
> Does that help?
> Ben Root

Sorry, didn't see that you have nested dtypes.  Is there a particular
reason why you need record arrays over structured arrays?

Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120215/1f3240ca/attachment.html 


More information about the NumPy-Discussion mailing list