[Numpy-discussion] bug with assignment into an indexed array?
Thu Aug 11 08:37:45 CDT 2011
Maybe confusing, but working as expected.
When you write:
matched_to[np.array([0, 1, 2])] = 3
it calls __setitem__ on matched_to, with arguments (np.array([0, 1, 2]), 3).
So numpy understand you want to write 3 at these indices.
When you write:
matched_to[:3][match] = 3
it first calls __getitem__ with the slice as argument, which returns a view
of your array, then it calls __setitem__ on this view, and it fills your
matched_to array at the same time.
But when you write:
matched_to[np.array([0, 1, 2])][match] = 3
it first calls __getitem__ with the array as argument, which retunrs a
*copy* of your array, so that calling __setitem__ on this copy has no effect
on your original array.
2011/8/10 Benjamin Root <email@example.com>
> Came across this today when trying to determine what was wrong with my
> import numpy as np
> matched_to = np.array([-1] * 5)
> in_ellipse = np.array([False, True, True, True, False])
> match = np.array([False, True, True])
> matched_to[in_ellipse][match] = 3
> I would expect matched_to to now be "array([-1, -1, 3, 3, -1])", but
> instead, it is still all -1.
> It would seem that unless the view was created by a slice, then the
> assignment into the indexed view would not work as expected. This works:
> >>> matched_to[:3][match] = 3
> but not:
> >>> matched_to[np.array([0, 1, 2])][match] = 3
> Note that the following does work:
> >>> matched_to[np.array([0, 1, 2])] = 3
> Is this a bug, or was I wrong to expect this to work this way?
> Ben Root
> NumPy-Discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion