Skip to content

Commit 836e7fe

Browse files
rhshadrachmroeschke
authored andcommitted
REGR: DataFrameGroupBy.agg with duplicate column names and a dict (pandas-dev#55042)
1 parent 5d8aa70 commit 836e7fe

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v2.1.1.rst

+1
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,13 @@ def compute_dict_like(
436436
Data for result. When aggregating with a Series, this can contain any
437437
Python object.
438438
"""
439+
from pandas.core.groupby.generic import (
440+
DataFrameGroupBy,
441+
SeriesGroupBy,
442+
)
443+
439444
obj = self.obj
445+
is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy))
440446
func = cast(AggFuncTypeDict, self.func)
441447
func = self.normalize_dictlike_arg(op_name, selected_obj, func)
442448

@@ -450,7 +456,7 @@ def compute_dict_like(
450456
colg = obj._gotitem(selection, ndim=1)
451457
results = [getattr(colg, op_name)(how, **kwargs) for _, how in func.items()]
452458
keys = list(func.keys())
453-
elif is_non_unique_col:
459+
elif not is_groupby and is_non_unique_col:
454460
# key used for column selection and output
455461
# GH#51099
456462
results = []

pandas/tests/groupby/aggregate/test_aggregate.py

+12
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,18 @@ def test_groupby_agg_dict_with_getitem():
515515
tm.assert_frame_equal(result, expected)
516516

517517

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

0 commit comments

Comments
 (0)