diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3da047eaa2d4e6..26a7fa3e8ca671 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5972,27 +5972,32 @@ def pipe( # Attribute access @final - def __finalize__(self, other, method: str | None = None, **kwargs) -> Self: - """ - Propagate metadata from other to self. - Parameters - ---------- - other : the object from which to get the attributes that we are going - to propagate - method : str, optional - A passed method name providing context on where ``__finalize__`` - was called. +@final +def __finalize__(self, other, method: str | None = None, **kwargs) -> Self: + """ + Propagate metadata from other to self. + + Parameters + ---------- + other : the object from which to get the attributes that we are going + to propagate + method : str, optional + A passed method name providing context on where ``__finalize__`` + was called. - .. warning:: + .. warning:: + + The value passed as `method` are not currently considered + stable across pandas releases. + """ + if isinstance(other, NDFrame): + if other.attrs: + self.attrs.update(other.attrs) + if hasattr(other, 'flags'): + self.flags = other.flags + return self - The value passed as `method` are not currently considered - stable across pandas releases. - """ - if isinstance(other, NDFrame): - if other.attrs: - # We want attrs propagation to have minimal performance - # 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)