Skip to content

Commit ab433af

Browse files
authored
REF: Add back attr passing in concat by attribute (#59195)
* REF: Add back attr passing in concat by attribute * define reference once
1 parent 58da4b0 commit ab433af

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6034,7 +6034,7 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
60346034
object.__setattr__(self, name, getattr(other, name, None))
60356035

60366036
if method == "concat":
6037-
objs = kwargs["objs"]
6037+
objs = other.objs
60386038
# propagate attrs only if all concat arguments have the same attrs
60396039
if all(bool(obj.attrs) for obj in objs):
60406040
# all concatenate arguments have non-empty attrs

pandas/core/reshape/concat.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
from collections import abc
8+
import types
89
from typing import (
910
TYPE_CHECKING,
1011
Literal,
@@ -536,7 +537,9 @@ def _get_result(
536537

537538
result = sample._constructor_from_mgr(mgr, axes=mgr.axes)
538539
result._name = name
539-
return result.__finalize__(object(), method="concat", objs=objs)
540+
return result.__finalize__(
541+
types.SimpleNamespace(objs=objs), method="concat"
542+
)
540543

541544
# combine as columns in a frame
542545
else:
@@ -556,7 +559,7 @@ def _get_result(
556559
)
557560
df = cons(data, index=index, copy=False)
558561
df.columns = columns
559-
return df.__finalize__(object(), method="concat", objs=objs)
562+
return df.__finalize__(types.SimpleNamespace(objs=objs), method="concat")
560563

561564
# combine block managers
562565
else:
@@ -595,7 +598,7 @@ def _get_result(
595598
)
596599

597600
out = sample._constructor_from_mgr(new_data, axes=new_data.axes)
598-
return out.__finalize__(object(), method="concat", objs=objs)
601+
return out.__finalize__(types.SimpleNamespace(objs=objs), method="concat")
599602

600603

601604
def new_axes(

pandas/tests/generic/test_frame.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ def finalize(self, other, method=None, **kwargs):
8484
value = getattr(left, name, "") + "|" + getattr(right, name, "")
8585
object.__setattr__(self, name, value)
8686
elif method == "concat":
87-
objs = kwargs["objs"]
8887
value = "+".join(
89-
[getattr(o, name) for o in objs if getattr(o, name, None)]
88+
[getattr(o, name) for o in other.objs if getattr(o, name, None)]
9089
)
9190
object.__setattr__(self, name, value)
9291
else:

pandas/tests/generic/test_series.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ def test_metadata_propagation_indiv(self, monkeypatch):
9494
def finalize(self, other, method=None, **kwargs):
9595
for name in self._metadata:
9696
if method == "concat" and name == "filename":
97-
objs = kwargs["objs"]
9897
value = "+".join(
99-
[getattr(obj, name) for obj in objs if getattr(obj, name, None)]
98+
[
99+
getattr(obj, name)
100+
for obj in other.objs
101+
if getattr(obj, name, None)
102+
]
100103
)
101104
object.__setattr__(self, name, value)
102105
else:

0 commit comments

Comments
 (0)