Skip to content

Backport PR #55042 on branch 2.1.x (REGR: DataFrameGroupBy.agg with duplicate column names and a dict) #55056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
- Fixed regression in :meth:`DataFrame.filter` not respecting the order of elements for ``filter`` (:issue:`54980`)
- Fixed regression in :meth:`DataFrame.to_sql` not roundtripping datetime columns correctly for sqlite (:issue:`54877`)
- Fixed regression in :meth:`DataFrameGroupBy.agg` when aggregating a DataFrame with duplicate column names using a dictionary (:issue:`55006`)
- Fixed regression in :meth:`MultiIndex.append` raising when appending overlapping :class:`IntervalIndex` levels (:issue:`54934`)
- Fixed regression in :meth:`Series.drop_duplicates` for PyArrow strings (:issue:`54904`)
- Fixed regression in :meth:`Series.interpolate` raising when ``fill_value`` was given (:issue:`54920`)
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,13 @@ def compute_dict_like(
Data for result. When aggregating with a Series, this can contain any
Python object.
"""
from pandas.core.groupby.generic import (
DataFrameGroupBy,
SeriesGroupBy,
)

obj = self.obj
is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy))
func = cast(AggFuncTypeDict, self.func)
func = self.normalize_dictlike_arg(op_name, selected_obj, func)

Expand All @@ -448,7 +454,7 @@ def compute_dict_like(
colg = obj._gotitem(selection, ndim=1)
results = [getattr(colg, op_name)(how, **kwargs) for _, how in func.items()]
keys = list(func.keys())
elif is_non_unique_col:
elif not is_groupby and is_non_unique_col:
# key used for column selection and output
# GH#51099
results = []
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ def test_groupby_agg_dict_with_getitem():
tm.assert_frame_equal(result, expected)


def test_groupby_agg_dict_dup_columns():
# GH#55006
df = DataFrame(
[[1, 2, 3, 4], [1, 3, 4, 5], [2, 4, 5, 6]],
columns=["a", "b", "c", "c"],
)
gb = df.groupby("a")
result = gb.agg({"b": "sum"})
expected = DataFrame({"b": [5, 4]}, index=Index([1, 2], name="a"))
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"op",
[
Expand Down