[Numpy-discussion] problem with assigning to recarrays
Pierre GM
pgmdevlist at gmail.com
Fri Feb 27 20:05:15 EST 2009
As a follow-up to Robert's answer:
>>> r[r.field1 == 1].field2 = 1
doesn't work, but
>>>r.field2[r.field1==1] = 1
does.
> So far, so good.
> Now I want to change the value of field2 for those same elements:
>
> In [128]: r[where(r.field1 == 1.)].field2 = 1
>
> Ok, so now the values of field 2 have been changed, for those elements
> right?
>
> In [129]: r.field2
>
> Out[129]: array([ 0., 0., 0., 0., 0.])
>
> Wait. What?
> That can't be right. Let's check again:
>
> In [130]: print r[where(r.field1 == 1.)].field2
> [ 0. 0.]
>
> Ok, so it appears that I can *access* fields in this array with an
> array of indices, but I can't assign new values to fields so
> accessed. However, I *can* change the values if I use a scalar
> index. This is different from the behavior of ordinary arrays, for
> which I can reassign elements' values either way.
>
> Moreover, when I try to reassign record array fields by indexing with
> an array of indices, it would appear that nothing at all happens.
> This syntax is equivalent to the pass command.
>
> So, my question is this: is there some reason for this behavior in
> record arrays, which is unexpectedly different from the behavior of
> normal arrays, and rather confusing. If so, why does the attempt to
> assign values to fields of an indexed subarray not raise some kind of
> error, rather than doing nothing? I think it's unlikely that I've
> actually found a bug in numpy, but this behavior does not make sense
> to me.
>
>
> Thanks for any insights,
>
> Brian
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list