Skip to content

Commit eb36970

Browse files
jasonmokkJason Mok
and
Jason Mok
authored
TST: Add test for duplicate by columns in groupby.agg using pd.namedAgg 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 <[email protected]>
1 parent 0c4799b commit eb36970

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pandas/tests/groupby/test_groupby.py

+31
Original file line numberDiff line numberDiff line change
@@ -2954,3 +2954,34 @@ def test_groupby_dropna_with_nunique_unique():
29542954
)
29552955

29562956
tm.assert_frame_equal(result, expected)
2957+
2958+
2959+
def test_groupby_agg_namedagg_with_duplicate_columns():
2960+
# GH#58446
2961+
df = DataFrame(
2962+
{
2963+
"col1": [2, 1, 1, 0, 2, 0],
2964+
"col2": [4, 5, 36, 7, 4, 5],
2965+
"col3": [3.1, 8.0, 12, 10, 4, 1.1],
2966+
"col4": [17, 3, 16, 15, 5, 6],
2967+
"col5": [-1, 3, -1, 3, -2, -1],
2968+
}
2969+
)
2970+
2971+
result = df.groupby(by=["col1", "col1", "col2"], as_index=False).agg(
2972+
new_col=pd.NamedAgg(column="col1", aggfunc="min"),
2973+
new_col1=pd.NamedAgg(column="col1", aggfunc="max"),
2974+
new_col2=pd.NamedAgg(column="col2", aggfunc="count"),
2975+
)
2976+
2977+
expected = DataFrame(
2978+
{
2979+
"col1": [0, 0, 1, 1, 2],
2980+
"col2": [5, 7, 5, 36, 4],
2981+
"new_col": [0, 0, 1, 1, 2],
2982+
"new_col1": [0, 0, 1, 1, 2],
2983+
"new_col2": [1, 1, 1, 1, 2],
2984+
}
2985+
)
2986+
2987+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)