Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
class MyDataFrame(pd.DataFrame):
_metadata = [
'name',
'extra_info',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = None
self.extra_info = {}
@property
def n_unique_wafer(self):
if 'wafer' in self.columns:
return len(self['wafer'].unique())
else:
return None
@property
def _constructor(self):
return MyDataFrame
if __name__ == "__main__":
data = {
'A': [1, 2, 3, 1, 2],
'B': ['A', 'B', 'A', 'C', 'B']
}
df = MyDataFrame(data)
df.extra_info = {"source": "Lab Experiment"}
# test copy()
copied_df = df.copy()
df.extra_info["source"] = 'a'
print("Extra Info:", copied_df.extra_info)
print("df Extra Info:", df.extra_info) # extra_info in df is changed
Issue Description
When using a custom subclass of pandas.DataFrame with additional metadata attributes (e.g., extra_info declared in _metadata), calling df.copy() or df.copy(deep=True) does not deep-copy the custom metadata attributes. Instead, the metadata remains shallow-copied, causing unintended shared references between the original and copied DataFrames.
Expected Behavior
Expected: copied_df.extra_info should retain the original value {"source": "Original Data"}. df.extra_info modifications should not affect the copy. Actual: Both the original and copied DataFrame share the same metadata object. df.copy(deep=True) does not deep-copy the _metadata attributes.
Installed Versions
Comment From: yuanx749
It seems this is expected according to the note in df.copy.
When deep=True, data is copied but actual Python objects will not be copied recursively, only the reference to the object.
Comment From: Qi-henry
So what should I do to avoid changing _metadata?