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 7 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
10 changes: 10 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5455,6 +5455,16 @@ 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
for obj in other.objs[1:]:
if obj.attrs != attrs:
break
else:
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
3 changes: 1 addition & 2 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@
(pd.Series, ([1, 2],), operator.methodcaller("convert_dtypes")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("convert_dtypes")),
marks=not_implemented_mark,
),
(pd.Series, ([1, None, 3],), operator.methodcaller("interpolate")),
(pd.DataFrame, ({"A": [1, None, 3]},), operator.methodcaller("interpolate")),
Expand Down Expand Up @@ -762,7 +761,7 @@ 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.apply(lambda y: y), Fixed with #42252
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure I understand what the changes in concat behavior have to do with this test.
Can you please clarify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, the apply will use concat internally

concatenated = concat(results, keys=keys, axis=1, sort=False)
.

Copy link
Member

Choose a reason for hiding this comment

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

Yet, I am not sure I understood the reason.
Maybe some other reviewers will catch the idea.

Copy link
Contributor Author

@xiki-tempula xiki-tempula Jun 30, 2021

Choose a reason for hiding this comment

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

@ivanovmg Sorry for the confusion. The test checks if apply will retain the attrs and mark this as failing as the attrs should get lost.

However, in this test, the apply calls concat, which will now preserve the attrs.
Though the function apply don't have a finaliser to ensure the pass of attrs, all the operations done inside apply will preserve the attrs. Thus, the final result will have attrs and thus, the test will no longer fail. So I have removed the mark as fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

don't comment out cases

lambda x: x.agg("std"),
lambda x: x.agg("var"),
lambda x: x.agg("sem"),
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,25 @@ 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():
# GH#41828
d = {'col1': [1, 2], 'col2': [3, 4]}
df1 = pd.DataFrame(data=d)
df1.attrs = {1: 1}
df2 = pd.DataFrame(data=d)
df2.attrs = {1: 1}
df = pd.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.

construct the appropraiate expected value then use tm.assert_frame_equal, you can also directly compare the attrs for clarity



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 = pd.DataFrame(data=d)
df1.attrs = {1: 1}
df2 = pd.DataFrame(data=d)
df2.attrs = {1: 2}
df = pd.concat([df1, df2])
assert df.attrs == {}