Skip to content

Commit

Permalink
TST: Add test for duplicate by columns in groupby.agg using `pd.n…
Browse files Browse the repository at this point in the history
…amedAgg` and `asIndex=False` (#58579)

* implement test for GH #58446

* Reformat GH issue comment

* Directly inline as_index=False in groupby call

---------

Co-authored-by: Jason Mok <jasonmok@Jasons-MacBook-Air-4.local>
  • Loading branch information
jasonmokk and Jason Mok committed May 6, 2024
1 parent 0c4799b commit eb36970
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Expand Up @@ -2954,3 +2954,34 @@ def test_groupby_dropna_with_nunique_unique():
)

tm.assert_frame_equal(result, expected)


def test_groupby_agg_namedagg_with_duplicate_columns():
# 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=False).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],
}
)

tm.assert_frame_equal(result, expected)

0 comments on commit eb36970

Please sign in to comment.