Following code is my simple example, for each step I explain what I'm doing in the comments, and the question is at the end.
import pandas as pd
todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date, periods=3, freq='D')
columns = ['A','B','C']
df1 = pd.DataFrame(index=index, columns=columns)
df1 = df1.fillna(1)
# up to here, i've just create a random df1, which looks like the follow:
# A B C
# 2020-03-24 1 1 1
# 2020-03-25 1 1 1
# 2020-03-26 1 1 1
df2 = df1 # here created a copy of df1 named as df2, it should pass by value based on my knowledge
df1 += df2.shift(1) # this should be the same as df1 = df1 + df2.shift(1)
display(df1) # here I print out df1 which looks like the follow:
# A B C
# 2020-03-24 NaN NaN NaN
# 2020-03-25 2.0 2.0 2.0
# 2020-03-26 2.0 2.0 2.0
display(df2) # here I print out df2, the result surprise me because i thought df2 isn't changed from when it is defined , however it becomes the same as the new df1:
# A B C
# 2020-03-24 NaN NaN NaN
# 2020-03-25 2.0 2.0 2.0
# 2020-03-26 2.0 2.0 2.0
Can anyone explain to me why df2
is changed in these steps? I'm really confused.