Hi-

I'm quite new to numpy and to python in general, so I apologize if I'm  
missing something obvious, but I've come across some seemingly nasty  
behavior when trying to assign values to the fields of an indexed  
subarray of a numpy record array.   Perhaps an example would explain  
it best.

First, I make a boring record array:

        In [124]: r = rec.fromarrays([zeros(5), zeros(5)],  
names='field1,field2')

This has five elements with two fields, all values are zero.
Now I can change the values for field1 for a few of the array elements:

        In [125]: r[1].field1=1

        In [126]: r[3].field1=1

Let's check and make sure that worked:

        In [127]: print r.field1
        [ 0. 1. 0. 1. 0.]

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@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to