Skip to content

ENH: allow attrs to be propagated via pd.concat #42252

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 25 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from 16 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
8 changes: 8 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5522,6 +5522,14 @@ def __finalize__(
object.__setattr__(self, name, getattr(other, name, None))

if method == "concat":
# Issue #41828, retain the attrs only if all NDFrame have the same
Copy link
Contributor

Choose a reason for hiding this comment

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

this is already handled above no? L5448

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback Thanks for the review. No.
So during concatenation, the other is a concatenator, which is not an NDFrame.
So it will not pass the if statement in L5447.

Copy link
Contributor

Choose a reason for hiding this comment

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

can remove the comment

# attrs.
attrs = other.objs[0].attrs
check_attrs = all(objs.attrs == attrs for objs in other.objs[1:])
if check_attrs:
for name in attrs:
self.attrs[name] = attrs[name]

allows_duplicate_labels = all(
x.flags.allows_duplicate_labels for x in other.objs
)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@
operator.methodcaller("infer_objects"),
),
(pd.Series, ([1, 2],), operator.methodcaller("convert_dtypes")),
(pd.DataFrame, frame_data, operator.methodcaller("convert_dtypes")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("convert_dtypes")),
),
(pd.Series, ([1, None, 3],), operator.methodcaller("interpolate")),
(pd.DataFrame, ({"A": [1, None, 3]},), operator.methodcaller("interpolate")),
(pd.Series, ([1, 2],), operator.methodcaller("clip", lower=1)),
Expand Down Expand Up @@ -759,7 +761,6 @@ def test_groupby_finalize(obj, method):
[
lambda x: x.agg(["sum", "count"]),
lambda x: x.transform(lambda y: y),
lambda x: x.apply(lambda y: y),
lambda x: x.agg("std"),
lambda x: x.agg("var"),
lambda x: x.agg("sem"),
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,36 @@ def test_concat_posargs_deprecation():
result = concat([df, df2], 0)
expected = DataFrame([[1, 2, 3], [4, 5, 6]], index=["a", "b"])
tm.assert_frame_equal(result, expected)


def test_concat_retain_attrs_df():
# GH#41828
d = {"col1": [1, 2], "col2": [3, 4]}
df1 = DataFrame(data=d)
df1.attrs = {1: 1}
df2 = DataFrame(data=d)
df2.attrs = {1: 1}
df = concat([df1, df2])
assert df.attrs[1] == 1
Copy link
Contributor

Choose a reason for hiding this comment

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

use assert_equal (u can also compare attrs if u want)

though should prob add a flag to do this in the asset_ routines (i don't think we have this)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback Thank you for the review. I wonder if do you mean pandas._testing.assert_equal? It seems that pandas._testing.assert_equal cannot handle comparison between integers.



def test_concat_retain_attrs_series():
Copy link
Contributor

Choose a reason for hiding this comment

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

paramterize over series and frame and use assert_equal

# GH#41828
d = [1, 2]
df1 = Series(data=d)
df1.attrs = {1: 1}
df2 = Series(data=d)
df2.attrs = {1: 1}
df = concat([df1, df2])
assert df.attrs[1] == 1


def test_concat_drop_attrs():
Copy link
Contributor

Choose a reason for hiding this comment

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

same

# GH#41828
d = {"col1": [1, 2], "col2": [3, 4]}
df1 = DataFrame(data=d)
df1.attrs = {1: 1}
df2 = DataFrame(data=d)
df2.attrs = {1: 2}
df = concat([df1, df2])
assert len(df.attrs) == 0