Skip to content

TST: Add test for duplicate by columns in groupby.agg using pd.namedAgg and asIndex=False #58579

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 6 commits into from
May 6, 2024
Merged
Changes from 4 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
35 changes: 35 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2954,3 +2954,38 @@ def test_groupby_dropna_with_nunique_unique():
)

tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("as_index_flag", [False])
Copy link
Member

Choose a reason for hiding this comment

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

You can just inline as_index=False in the groupby call below

Copy link
Contributor Author

@jasonmokk jasonmokk May 6, 2024

Choose a reason for hiding this comment

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

Got it, I've updated the PR!

def test_groupby_agg_namedagg_with_duplicate_columns(as_index_flag):
# GH#58446
df = DataFrame(
{
"col1": [2, 1, 1, 0, 2, 0],
"col2": [4, 5, 36, 7, 4, 5],
"col3": [3.1, 8.0, 12, 10, 4, 1.1],
"col4": [17, 3, 16, 15, 5, 6],
"col5": [-1, 3, -1, 3, -2, -1],
}
)

result = df.groupby(by=["col1", "col1", "col2"], as_index=as_index_flag).agg(
new_col=pd.NamedAgg(column="col1", aggfunc="min"),
new_col1=pd.NamedAgg(column="col1", aggfunc="max"),
new_col2=pd.NamedAgg(column="col2", aggfunc="count"),
)

expected = DataFrame(
{
"col1": [0, 0, 1, 1, 2],
"col2": [5, 7, 5, 36, 4],
"new_col": [0, 0, 1, 1, 2],
"new_col1": [0, 0, 1, 1, 2],
"new_col2": [1, 1, 1, 1, 2],
}
)

if not as_index_flag:
expected.reset_index(drop=True, inplace=True)

tm.assert_frame_equal(result, expected)
Loading