BUG: copy.deepcopy()
doesn't deepcopy the metadata in .attrs
· Issue #54134 · pandas-dev/pandas (original) (raw)
Pandas version checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd import copy
Create a GeoDataFrame
df = pd.DataFrame({ 'value': [1, 2] })
Add an attribute to the DataFrame
df.attrs['transform'] = {}
Copy the DataFrame
df2 = copy.deepcopy(df)
gdf2 = gdf.copy(deep=True)
Print the IDs
print(id(df.attrs['transform'])) # ID of copied attribute print(id(df2.attrs['transform'])) # ID of original attribute
Assertion
assert df.attrs['transform'] is not df2.attrs['transform']
Issue Description
I would expect the dict in .attrs to be a copy since I performed a deep copy, but it is still the same object. Originally I though it was a bug of geopandas
, but it is a bug of pandas
. For cross-ref, this is my original issue: geopandas/geopandas#2920 (comment)
Expected Behavior
The assertion should not fail.
Installed Versions
pd.show_versions()
/Users/macbook/miniconda3/envs/ome/lib/python3.10/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
INSTALLED VERSIONS
commit : 2e218d1
python : 3.10.12.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.5.3
numpy : 1.23.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.1.2
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.7.2
numba : 0.57.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 12.0.1
pyreadstat : None
pyxlsb : None
s3fs : 2023.6.0
scipy : 1.11.1
snappy : None
sqlalchemy : 2.0.18
tables : None
tabulate : 0.9.0
xarray : 2022.12.0
xlrd : None
xlwt : None
zstandard : None
tzdata : 2023.3