Trying to mutate a slice of an array, adding a constant to elements with odd indices. But this must be a copy, not the original array which is assigned, since this doesn’t change the array:
times = np.arange(10)
times[1::2] = times[1::2] + 0.5
It’s curious since this works:
times[1::2] = -1
Why is the slice a copy?
How to make it work like expected?
>Solution :
Use addition assignment (with type agreement):
a = np.arange(10).astype(float)
a[1::2] += 0.5
array([0. , 1.5, 2. , 3.5, 4. , 5.5, 6. , 7.5, 8. , 9.5])