Skip to content

Commit

Permalink
implement test for GH pandas-dev#58446
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Mok authored and Jason Mok committed May 5, 2024
1 parent d9a02be commit 03e45b0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pandas/tests/groupby/test_groupby.py
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])
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)

0 comments on commit 03e45b0

Please sign in to comment.