I know the shallow copy behaviour of list
list1 = [1, 2, 3, 4]
list2 = copy(list1) # shallow copy of list1
list1 is list2 # False as expected
False
list1[0] is list2[0] # True as expected
True
list1[0] = 10 # this will change list1 but not list2
# the result is as expected
list1
[10, 2, 3, 4]
list2
[1, 2, 3, 4]
I have tried similar operations with pandas
series
. The behaviour is different.
series1 = pd.Series([1, 2, 3, 4])
series2 = series1.copy(deep=False) # shallow copy
series1 is series2 # as expected
False
series1[0] is series2[0] # Q1: I was expecting True
False
# Q2: I was expecting the change in series1 but not in series2
series1[0] = 10
series1
0 10
1 2
2 3
3 4
dtype: int64
series2
0 10
1 2
2 3
3 4
dtype: int64
Question 1: Why series1[0]
and series2[0]
is not the same?
Question 2: Why changing first element of series1
also changed series2
even though they are different as per Question 1?
Why the behaviour is not consistent for list
and series
? Is there any special reason for this?