Skip to content

REF: Add back attr passing in concat by attribute #59195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly more performant to define objs=other.objs once?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of kwargs? Will this mess up for subclasssss?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly more performant to define objs=other.objs once?

Sure thing. Make sense

Can we get rid of kwargs? Will this mess up for subclasssss?

Looks generally unused but not exactly sure the scope of how this is used. I'd prefer to maybe do this in a separate change

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
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

from collections import abc
import types
from typing import (
TYPE_CHECKING,
Literal,
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down