If
(A) Mutable objects modified in functions are also mutated in the calling context
and
(B) pandas dataframes are mutable objects,
then in the following example, why is an empty dataframe not printed in the last output (Outside-After
)?
import pandas as pd
def foo(df):
df=df[0:0] # clear the df
print(df)
df=pd.DataFrame([[1,2,3],[4,5,6]])
print("\nOutside - Before:")
print(df)
print("\nInside function:")
foo(df)
print("\nOutside - After:")
print(df)
Output:
Outside - Before:
0 1 2
0 1 2 3
1 4 5 6
Inside function:
Empty DataFrame
Columns: [0, 1, 2]
Index: []
Outside - After:
0 1 2
0 1 2 3
1 4 5 6