diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 312f5d20d794f..0fc475cfa62ae 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6017,7 +6017,7 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self: object.__setattr__(self, name, getattr(other, name, None)) if method == "concat": - objs = kwargs["objs"] + objs = other.objs # propagate attrs only if all concat arguments have the same attrs if all(bool(obj.attrs) for obj in objs): # all concatenate arguments have non-empty attrs diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 6381869c3e559..6836ba3f65691 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -5,6 +5,7 @@ from __future__ import annotations from collections import abc +import types from typing import ( TYPE_CHECKING, Literal, @@ -536,7 +537,9 @@ def _get_result( result = sample._constructor_from_mgr(mgr, axes=mgr.axes) result._name = name - return result.__finalize__(object(), method="concat", objs=objs) + return result.__finalize__( + types.SimpleNamespace(objs=objs), method="concat" + ) # combine as columns in a frame else: @@ -556,7 +559,7 @@ def _get_result( ) df = cons(data, index=index, copy=False) df.columns = columns - return df.__finalize__(object(), method="concat", objs=objs) + return df.__finalize__(types.SimpleNamespace(objs=objs), method="concat") # combine block managers else: @@ -595,7 +598,7 @@ def _get_result( ) out = sample._constructor_from_mgr(new_data, axes=new_data.axes) - return out.__finalize__(object(), method="concat", objs=objs) + return out.__finalize__(types.SimpleNamespace(objs=objs), method="concat") def new_axes( diff --git a/pandas/tests/generic/test_frame.py b/pandas/tests/generic/test_frame.py index d06bfad930d7c..1d0f491529b56 100644 --- a/pandas/tests/generic/test_frame.py +++ b/pandas/tests/generic/test_frame.py @@ -84,9 +84,8 @@ def finalize(self, other, method=None, **kwargs): value = getattr(left, name, "") + "|" + getattr(right, name, "") object.__setattr__(self, name, value) elif method == "concat": - objs = kwargs["objs"] value = "+".join( - [getattr(o, name) for o in objs if getattr(o, name, None)] + [getattr(o, name) for o in other.objs if getattr(o, name, None)] ) object.__setattr__(self, name, value) else: diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 9e2dae8d132eb..7dcdcd96cce51 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -94,9 +94,12 @@ def test_metadata_propagation_indiv(self, monkeypatch): def finalize(self, other, method=None, **kwargs): for name in self._metadata: if method == "concat" and name == "filename": - objs = kwargs["objs"] value = "+".join( - [getattr(obj, name) for obj in objs if getattr(obj, name, None)] + [ + getattr(obj, name) + for obj in other.objs + if getattr(obj, name, None) + ] ) object.__setattr__(self, name, value) else: