I have seen a phenomenon recently in working with structured numpy arrays that doesn't make sense. I am hoping someone can help me understand what is going on. I have provided a minimal working example to illustrate the problem. The problem is this:
When indexing a structured numpy array with a boolean mask, this works:
arr['fieldName'][boolMask] += val
but the following does not:
arr[boolMask]['fieldName'] += val
Here is a minimal working example:
import numpy as np
myDtype = np.dtype([('t','<f8'),('p','<f8',(3,)),('v','<f4',(3,))])
nominalArray = np.zeros((10,),dtype=myDtype)
nominalArray['t'] = np.arange(10.)
# In real life, the other fields would also be populated
print "original times: {0}".format(nominalArray['t'])
# Add 10 to all times greater than 5
timeGreaterThan5 = nominalArray['t'] > 5
nominalArray['t'][timeGreaterThan5] += 10.
print "times after first operation: {0}".format(nominalArray['t'])
# Return those times to their original values
nominalArray[timeGreaterThan5]['t'] -= 10.
print "times after second operation: {0}".format(nominalArray['t'])
Running this yields the following output:
original times: [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
times after first operation: [ 0. 1. 2. 3. 4. 5. 16. 17. 18. 19.]
times after second operation: [ 0. 1. 2. 3. 4. 5. 16. 17. 18. 19.]
We clearly see here that the second operation had no effect. If somebody could explain why this occurs, it would be greatly appreciated.