Skip to content

Commit 2f0ef87

Browse files
Backport PR #55042 on branch 2.1.x (REGR: DataFrameGroupBy.agg with duplicate column names and a dict) (#55056)
Backport PR #55042: REGR: DataFrameGroupBy.agg with duplicate column names and a dict Co-authored-by: Richard Shadrach <[email protected]>
1 parent 610c0f4 commit 2f0ef87

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v2.1.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
2222
- Fixed regression in :meth:`DataFrame.filter` not respecting the order of elements for ``filter`` (:issue:`54980`)
2323
- Fixed regression in :meth:`DataFrame.to_sql` not roundtripping datetime columns correctly for sqlite (:issue:`54877`)
24+
- Fixed regression in :meth:`DataFrameGroupBy.agg` when aggregating a DataFrame with duplicate column names using a dictionary (:issue:`55006`)
2425
- Fixed regression in :meth:`MultiIndex.append` raising when appending overlapping :class:`IntervalIndex` levels (:issue:`54934`)
2526
- Fixed regression in :meth:`Series.drop_duplicates` for PyArrow strings (:issue:`54904`)
2627
- Fixed regression in :meth:`Series.interpolate` raising when ``fill_value`` was given (:issue:`54920`)

pandas/core/apply.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,13 @@ def compute_dict_like(
434434
Data for result. When aggregating with a Series, this can contain any
435435
Python object.
436436
"""
437+
from pandas.core.groupby.generic import (
438+
DataFrameGroupBy,
439+
SeriesGroupBy,
440+
)
441+
437442
obj = self.obj
443+
is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy))
438444
func = cast(AggFuncTypeDict, self.func)
439445
func = self.normalize_dictlike_arg(op_name, selected_obj, func)
440446

@@ -448,7 +454,7 @@ def compute_dict_like(
448454
colg = obj._gotitem(selection, ndim=1)
449455
results = [getattr(colg, op_name)(how, **kwargs) for _, how in func.items()]
450456
keys = list(func.keys())
451-
elif is_non_unique_col:
457+
elif not is_groupby and is_non_unique_col:
452458
# key used for column selection and output
453459
# GH#51099
454460
results = []

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,18 @@ def test_groupby_agg_dict_with_getitem():
513513
tm.assert_frame_equal(result, expected)
514514

515515

516+
def test_groupby_agg_dict_dup_columns():
517+
# GH#55006
518+
df = DataFrame(
519+
[[1, 2, 3, 4], [1, 3, 4, 5], [2, 4, 5, 6]],
520+
columns=["a", "b", "c", "c"],
521+
)
522+
gb = df.groupby("a")
523+
result = gb.agg({"b": "sum"})
524+
expected = DataFrame({"b": [5, 4]}, index=Index([1, 2], name="a"))
525+
tm.assert_frame_equal(result, expected)
526+
527+
516528
@pytest.mark.parametrize(
517529
"op",
518530
[

0 commit comments

Comments
 (0)