Skip to content

ENH: Enable destination aware scope for dataset attributes metadata propagation copy routines #62131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
7 changes: 5 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6112,7 +6112,10 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
# impact if attrs are not used; i.e. attrs is an empty dict.
# One could make the deepcopy unconditionally, but a deepcopy
# of an empty dict is 50x more expensive than the empty check.
self.attrs = deepcopy(other.attrs)
# We provide the new dataset via the deepcopy memo to properly
# supply eventual attribute copy routines requiring information
# from its destination.
self.attrs = deepcopy(other.attrs, memo={id(self): self})
self.flags.allows_duplicate_labels = (
self.flags.allows_duplicate_labels
and other.flags.allows_duplicate_labels
Expand All @@ -6130,7 +6133,7 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
attrs = objs[0].attrs
have_same_attrs = all(obj.attrs == attrs for obj in objs[1:])
if have_same_attrs:
self.attrs = deepcopy(attrs)
self.attrs = deepcopy(attrs, memo={id(self): self})

allows_duplicate_labels = all(x.flags.allows_duplicate_labels for x in objs)
self.flags.allows_duplicate_labels = allows_duplicate_labels
Expand Down
Loading