[Numpy-discussion] Record arrays, nesting, and assignment

Pauli Virtanen pav+sp@iki...
Wed Jul 15 14:38:36 CDT 2009


On 2009-07-15, Robert Kern <robert.kern@gmail.com> wrote:
> On Wed, Jul 15, 2009 at 14:19, Vebjorn Ljosa<vebjorn@ljosa.com> wrote:
>> Suppose I have a record array where one of the fields is a nested array:
>>
>>  >>> from numpy import *
>>  >>> desc = dtype([('point', 'i4', 3), ('unimportant', 'S3')])
>>  >>> a = array([((1,2,3), 'foo'), ((7,8,9), 'bar')], dtype=desc)
>>  >>> a
>> array([([1, 2, 3], 'foo'), ([7, 8, 9], 'bar')],
>>      dtype=[('point', '<i4', 3), ('unimportant', '|S3')])
>>  >>> a[0]
>> ([1, 2, 3], 'foo')
>>
>> If I try to assign to a[0]['point'], it only works partially:
>>
>>  >>> a[0]['point'] = array([4, 5, 6])
>>  >>> a[0]
>> ([4, 2, 3], 'foo')
>
[clip]
>
> Generally, scalars are never views. a[0] pulls out a record scalar.
> Assigning into that scalar does not affect the original memory.
> a['point'] creates an array that is a view and assigning to the [0]
> element of that array modifies the original memory.

But then, why does it alter the first element of the sub-array?
This seems like a bug...

-- 
Pauli Virtanen



More information about the NumPy-Discussion mailing list