Open achapkowski opened 6 months ago
To follow up, this PR https://github.com/pandas-dev/pandas/pull/55314 is the breaking change.
As outlined in the PR, deepcopy was introduced to prevent unwanted side effects when attrs are used and propagated.
The failure mode of using a class without __deepcopy__
is transparent, just like the fix required (implement deepcopy) to be able to use attrs with the custom class.
On the other hand, side effects from not deepcopy'ing attrs in the background are intransparent to the end user. So in the end using deepcopy is probably preferable.
@achapkowski Maybe try df.attrs["metadata"] = {**InsensitiveDict(data)}
.
@achapkowski Since you are making a custom implementation of a dictionary, you can implement a __deepcopy__
like this:
def __deepcopy__(self, memo):
result = InsensitiveDict(deepcopy({**self}))
memo[id(self)] = result
return result
I know I can implement deepcopy, but the issue is if I use any 3rd party library, like this class from requests, I have to monkey patch it.
How is it problematic? From what I read it's a problem with geopandas not pandas. This is a major regression because most classes and packages do not implement deepcopy.
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
Issue Description
In Pandas 2.1 and below, we could put whatever we wanted in the
attrs
and the data would not throw a__deepcopy__
error. We would like it if theattrs
didn't require information to be deep copied like previous version.https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.attrs.html
Expected Behavior
When working with data and you have any custom class that doesn't implement
__deepcopy__
, the pandas data is essentially useless on any operation.Installed Versions