Skip to content

Commit

Permalink
Backport PR #55042 on branch 2.1.x (REGR: DataFrameGroupBy.agg with d…
Browse files Browse the repository at this point in the history
…uplicate column names and a dict) (#55056)

Backport PR #55042: REGR: DataFrameGroupBy.agg with duplicate column names and a dict

Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and rhshadrach committed Sep 7, 2023
1 parent 610c0f4 commit 2f0ef87
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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

0 comments on commit 2f0ef87

Please sign in to comment.