Skip to content

Commit 3d0e66a

Browse files
Charlie-XIAOim-vinicius
authored and
im-vinicius
committed
BUG DataFrameGroupBy.agg with list not respecting as_index=False (pandas-dev#53237)
* fix dataframegroupby.agg not respecting as_index=false * cleaned up code, removed breakpoints * cleaning up codes * final cleanup of code, ready for review, sry abt that * resolve typing (not sure) * used better approach * new test added; enh short circuiting * clean up code
1 parent 609d7b4 commit 3d0e66a

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ Groupby/resample/rolling
422422
grouped :class:`Series` or :class:`DataFrame` was a :class:`DatetimeIndex`, :class:`TimedeltaIndex`
423423
or :class:`PeriodIndex`, and the ``groupby`` method was given a function as its first argument,
424424
the function operated on the whole index rather than each element of the index. (:issue:`51979`)
425+
- Bug in :meth:`DataFrameGroupBy.agg` with lists not respecting ``as_index=False`` (:issue:`52849`)
425426
- Bug in :meth:`DataFrameGroupBy.apply` causing an error to be raised when the input :class:`DataFrame` was subset as a :class:`DataFrame` after groupby (``[['a']]`` and not ``['a']``) and the given callable returned :class:`Series` that were not all indexed the same. (:issue:`52444`)
426427
- Bug in :meth:`DataFrameGroupBy.apply` raising a ``TypeError`` when selecting multiple columns and providing a function that returns ``np.ndarray`` results (:issue:`18930`)
427428
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)

pandas/core/groupby/generic.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
is_bool,
4545
is_dict_like,
4646
is_integer_dtype,
47+
is_list_like,
4748
is_numeric_dtype,
4849
is_scalar,
4950
)
@@ -1397,7 +1398,11 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
13971398
op = GroupByApply(self, func, args=args, kwargs=kwargs)
13981399
result = op.agg()
13991400
if not is_dict_like(func) and result is not None:
1400-
return result
1401+
# GH #52849
1402+
if not self.as_index and is_list_like(func):
1403+
return result.reset_index()
1404+
else:
1405+
return result
14011406
elif relabeling:
14021407
# this should be the only (non-raising) case with relabeling
14031408
# used reordered index of columns

pandas/tests/groupby/aggregate/test_aggregate.py

+13
Original file line numberDiff line numberDiff line change
@@ -1586,3 +1586,16 @@ def test_agg_multiple_with_as_index_false_subset_to_a_single_column():
15861586
result = gb.agg(["sum", "mean"])
15871587
expected = DataFrame({"a": [1, 2], "sum": [7, 5], "mean": [3.5, 5.0]})
15881588
tm.assert_frame_equal(result, expected)
1589+
1590+
1591+
def test_agg_with_as_index_false_with_list():
1592+
# GH#52849
1593+
df = DataFrame({"a1": [0, 0, 1], "a2": [2, 3, 3], "b": [4, 5, 6]})
1594+
gb = df.groupby(by=["a1", "a2"], as_index=False)
1595+
result = gb.agg(["sum"])
1596+
1597+
expected = DataFrame(
1598+
data=[[0, 2, 4], [0, 3, 5], [1, 3, 6]],
1599+
columns=MultiIndex.from_tuples([("a1", ""), ("a2", ""), ("b", "sum")]),
1600+
)
1601+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_categorical.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2067,11 +2067,8 @@ def test_agg_list(request, as_index, observed, reduction_func, test_series, keys
20672067
if as_index and (test_series or reduction_func == "size"):
20682068
expected = expected.to_frame(reduction_func)
20692069
if not test_series:
2070-
if not as_index:
2071-
# TODO: GH#52849 - as_index=False is not respected
2072-
expected = expected.set_index(keys)
2073-
expected.columns = MultiIndex(
2074-
levels=[["b"], [reduction_func]], codes=[[0], [0]]
2070+
expected.columns = MultiIndex.from_tuples(
2071+
[(ind, "") for ind in expected.columns[:-1]] + [("b", reduction_func)]
20752072
)
20762073
elif not as_index:
20772074
expected.columns = keys + [reduction_func]

0 commit comments

Comments
 (0)