When I have a structured masked array with boolean indexing, under what conditions do I get a view and when do I get a copy? The documentation says that advanced indexing always returns a copy, but this is not true, since something like X[X>0]=42
is technically advanced indexing, but the assignment works. My situation is more complex:
I want to set the mask of a particular field based on a criterion from another field, so I need to get the field, apply the boolean indexing, and get the mask. There are 3! = 6 orders of doing so.
Preparation:
In [83]: M = ma.MaskedArray(random.random(400).view("f8,f8,f8,f8")).reshape(10, 10)
In [84]: crit = M[:, 4]["f2"] > 0.5
Field - index - mask (fails):
In [85]: M["f3"][crit, 3].mask = True In [86]: print(M["f3"][crit, 3].mask) [False False False False False]
Index - field - mask (fails):
In [87]: M[crit, 3]["f3"].mask = True In [88]: print(M[crit, 3]["f3"].mask) [False False False False False]
Index - mask - field (fails):
In [94]: M[crit, 3].mask["f3"] = True In [95]: print(M[crit, 3].mask["f3"]) [False False False False False]
Mask - index - field (fails):
In [101]: M.mask[crit, 3]["f3"] = True In [102]: print(M.mask[crit, 3]["f3"]) [False False False False False]
Field - mask - index (succeeds):
In [103]: M["f3"].mask[crit, 3] = True In [104]: print(M["f3"].mask[crit, 3]) [ True True True True True] # set back to False so I can try method #6 In [105]: M["f3"].mask[crit, 3] = False In [106]: print(M["f3"].mask[crit, 3]) [False False False False False]
Mask - field - index (succeeds):
In [107]: M.mask["f3"][crit, 3] = True In [108]: print(M.mask["f3"][crit, 3]) [ True True True True True]
So, it looks like indexing must come last.