From 6f1f127ac3edd60c3e2e2931525c2f06d658ce2a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Sat, 6 Jul 2024 09:27:11 -0700 Subject: [PATCH 1/2] REF: Add back attr passing in concat by attribute --- pandas/core/generic.py | 11 ++++++----- pandas/core/reshape/concat.py | 9 ++++++--- pandas/tests/generic/test_frame.py | 3 +-- pandas/tests/generic/test_series.py | 7 +++++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 312f5d20d794f..33190f905be13 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6017,16 +6017,17 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self: object.__setattr__(self, name, getattr(other, name, None)) if method == "concat": - objs = kwargs["objs"] # propagate attrs only if all concat arguments have the same attrs - if all(bool(obj.attrs) for obj in objs): + if all(bool(obj.attrs) for obj in other.objs): # all concatenate arguments have non-empty attrs - attrs = objs[0].attrs - have_same_attrs = all(obj.attrs == attrs for obj in objs[1:]) + attrs = other.objs[0].attrs + have_same_attrs = all(obj.attrs == attrs for obj in other.objs[1:]) if have_same_attrs: self.attrs = deepcopy(attrs) - allows_duplicate_labels = all(x.flags.allows_duplicate_labels for x in objs) + allows_duplicate_labels = all( + x.flags.allows_duplicate_labels for x in other.objs + ) self.flags.allows_duplicate_labels = allows_duplicate_labels return self 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: From 63fba01f232b806481baec1e058201a25ba70e27 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 8 Jul 2024 08:27:55 -0700 Subject: [PATCH 2/2] define reference once --- pandas/core/generic.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 33190f905be13..0fc475cfa62ae 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6017,17 +6017,16 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self: object.__setattr__(self, name, getattr(other, name, None)) if method == "concat": + objs = other.objs # propagate attrs only if all concat arguments have the same attrs - if all(bool(obj.attrs) for obj in other.objs): + if all(bool(obj.attrs) for obj in objs): # all concatenate arguments have non-empty attrs - attrs = other.objs[0].attrs - have_same_attrs = all(obj.attrs == attrs for obj in other.objs[1:]) + attrs = objs[0].attrs + have_same_attrs = all(obj.attrs == attrs for obj in objs[1:]) if have_same_attrs: self.attrs = deepcopy(attrs) - allows_duplicate_labels = all( - x.flags.allows_duplicate_labels for x in other.objs - ) + allows_duplicate_labels = all(x.flags.allows_duplicate_labels for x in objs) self.flags.allows_duplicate_labels = allows_duplicate_labels return self