[Numpy-discussion] Conditional update of recarray field

Bartosz mail@telenczuk...
Wed Nov 28 06:47:18 CST 2012


Hi,

I try to update values in a single field of numpy record array based on 
a condition defined in another array. I found that that the result 
depends on the order in which I apply the boolean indices/field names.

For example:

cond = np.zeros(5, dtype=np.bool)
cond[2:] = True
X = np.rec.fromarrays([np.arange(5)], names='a')
X[cond]['a'] = -1
print X

returns:  [(0,) (1,) (2,) (3,) (4,)] (the values were not updated)

X['a'][cond] = -1
print X

returns: [(0,) (1,) (-1,) (-1,) (-1,)] (it worked this time).

I find this behaviour very confusing. Is it expected? Would it be 
possible to emit a warning message in the case of "faulty" assignments?

Bartosz


More information about the NumPy-Discussion mailing list