1

I've been puzzled with unexpected Python behavior: when I make a copy of my original numpy array and replace some of its elements with a different value, the corresponding elements of my original array gets updated, too. Here's a simple test:

>>import numpy as np
>>x = np.array([0,0,2,2])
>>x_adj = x
>>x_adj[x_adj <= 0] = 1
>>print x
>>print x_adj
  [1 1 2 2]
  [1 1 2 2]

I wonder, why the original array gets updated, too, and how to keep it intact so changes will be made to the copy only. Any feedback is welcome!

koch
  • 97
  • 1
  • 6
  • This is standard behavior in python. The copy is a reference to the original. Use `deepcopy` in standard python or `.copy` in numpy. – roadrunner66 Mar 19 '16 at 20:37
  • Thanks for explaining! So is the referencing to the original when using '=' common across all python libraries (core, numpy, pandas) and for all objects (lists, arrays, data frames)? – koch Mar 19 '16 at 20:46
  • I don't want to overstate, but everything that's based on an object and everything that I'm aware off. It should be your default assumption. – roadrunner66 Mar 19 '16 at 22:03
  • Thanks, good rule of thumb. – koch Mar 20 '16 at 00:17

1 Answers1

1

Assignment is not a copy of an object in numpy. You are just coping the reference to an object, to make a copy of actual array use

x_adj = x.copy()

you can easily check it through id function

>>> import numpy as np
>>> x = np.array([0])
>>> print id(x)
140686120123168
>>> x_adj = x
>>> print id(x_adj)
140686120123168
>>> print id(x.copy())
140685864181632
lejlot
  • 64,777
  • 8
  • 131
  • 164