Skip to content

Commit 6f1f127

Browse files
committed
REF: Add back attr passing in concat by attribute
1 parent 236d89b commit 6f1f127

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

pandas/core/generic.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6017,16 +6017,17 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
60176017
object.__setattr__(self, name, getattr(other, name, None))
60186018

60196019
if method == "concat":
6020-
objs = kwargs["objs"]
60216020
# propagate attrs only if all concat arguments have the same attrs
6022-
if all(bool(obj.attrs) for obj in objs):
6021+
if all(bool(obj.attrs) for obj in other.objs):
60236022
# all concatenate arguments have non-empty attrs
6024-
attrs = objs[0].attrs
6025-
have_same_attrs = all(obj.attrs == attrs for obj in objs[1:])
6023+
attrs = other.objs[0].attrs
6024+
have_same_attrs = all(obj.attrs == attrs for obj in other.objs[1:])
60266025
if have_same_attrs:
60276026
self.attrs = deepcopy(attrs)
60286027

6029-
allows_duplicate_labels = all(x.flags.allows_duplicate_labels for x in objs)
6028+
allows_duplicate_labels = all(
6029+
x.flags.allows_duplicate_labels for x in other.objs
6030+
)
60306031
self.flags.allows_duplicate_labels = allows_duplicate_labels
60316032

60326033
return self

pandas/core/reshape/concat.py

Lines changed: 6 additions & 3 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 5 additions & 2 deletions
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)