This is because arange(5) returns an array of integers, but nan is a floating point value. When you are a normal destination, this is normal, because x + nan transparently converts x to float to perform the addition and returns the result of the float. But with += it tries to return this float result to the original x , which is an int array. This fails because the int array cannot accept floating point data.
Using += with numpy arrays updates the array in place, and this will not work if the result of your calculation is different from the data type than the original.
source share