I came across something i find very odd: Apparently it is not possible, to truly deep copy pandas dataframes.
I would expect, that if i create a deep copy of a dataframe, and i modify data in this copy, it has not effect on the original dataframe. But apparently this is not the case, or even possible if i'm not wrong.
Code to reproduce:
import pandas as pd
df = pd.DataFrame({'sets':set([1,2])}, index=[0])
def pop(df_in):
df = df_in.copy()
print(df['sets'].apply(lambda x: set([x.pop()])))
pop(df)
pop(df)
pop(df)
>>> KeyError: 'pop from an empty set'
or
import copy
import pandas as pd
df = pd.DataFrame({'sets':set([1,2])}, index=[0])
def pop(df_in):
df = copy.deepcopy(df_in)
print(df['sets'].apply(lambda x: set([x.pop()])))
pop(df)
pop(df)
pop(df)
>>> KeyError: 'pop from an empty set'
My questions are:
- Is it possible to create true deep copies of pandas dataframes?
- If not why? if yes, how?