Skip to content

Commit 1e283bf

Browse files
author
root
committed
Fix for issue pandas-dev#28283: Ensure __finalize__ propagates metadata correctly
1 parent 283a2dc commit 1e283bf

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

pandas/core/generic.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,27 +5972,32 @@ def pipe(
59725972
# Attribute access
59735973

59745974
@final
5975-
def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
5976-
"""
5977-
Propagate metadata from other to self.
59785975

5979-
Parameters
5980-
----------
5981-
other : the object from which to get the attributes that we are going
5982-
to propagate
5983-
method : str, optional
5984-
A passed method name providing context on where ``__finalize__``
5985-
was called.
5976+
@final
5977+
def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
5978+
"""
5979+
Propagate metadata from other to self.
5980+
5981+
Parameters
5982+
----------
5983+
other : the object from which to get the attributes that we are going
5984+
to propagate
5985+
method : str, optional
5986+
A passed method name providing context on where ``__finalize__``
5987+
was called.
59865988
5987-
.. warning::
5989+
.. warning::
5990+
5991+
The value passed as `method` are not currently considered
5992+
stable across pandas releases.
5993+
"""
5994+
if isinstance(other, NDFrame):
5995+
if other.attrs:
5996+
self.attrs.update(other.attrs)
5997+
if hasattr(other, 'flags'):
5998+
self.flags = other.flags
5999+
return self
59886000

5989-
The value passed as `method` are not currently considered
5990-
stable across pandas releases.
5991-
"""
5992-
if isinstance(other, NDFrame):
5993-
if other.attrs:
5994-
# We want attrs propagation to have minimal performance
5995-
# impact if attrs are not used; i.e. attrs is an empty dict.
59966001
# One could make the deepcopy unconditionally, but a deepcopy
59976002
# of an empty dict is 50x more expensive than the empty check.
59986003
self.attrs = deepcopy(other.attrs)

0 commit comments

Comments
 (0)