Michael
Michael

Reputation: 7809

by-value or by-reference with my_ndarray[...]

I try to copy an numpy.ndarray by value, the following way:

my_copy = my_original[...]

However, this seems to assign the reference.

While the following way does actually copy the data:

my_copy[...] = my_original

This confuses me, because I always assumed that [...] will just refer to the plain data, so that both ways should work.

What exactly is the rule and the reason for those behaviors? What's the best practice to copy data in Numpy?

Upvotes: 0

Views: 37

Answers (1)

MSeifert
MSeifert

Reputation: 152667

You are mixing two concepts, the first one:

y = x[...]

just assigns a view of x to the variable name y. x and y are not identical but they share the same memory (so changes will propagate to the other one).

y[...] = x

Assigns the values of x to an existing array y. This will copy the values if x and y don't share memory!

In general you should just use np.copy or np.ndarray.copy when you want to create a new copy of an array. If you want to copy an array into an existing array you need to make sure you don't lose the reference to the array you want to copy the values into. So you must not re-assign to the variable name (so y[:] = x or y[...] = x are fine because you work on the contents of y - but y = x just "overwrites" the variable name, it does not modify the contents of the original y).

Upvotes: 2

Related Questions