Skip to content
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Plotting
Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^

-
- Fixed bug in GroupBy when using as_index=False (issue:`25011`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in ::meth:`pandas.core.groupby.GroupBy.aggwhen passingas_index=False``, returning an additional column.

-
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def is_any_frame():
# we require a list, but not an 'str'
return self._aggregate_multiple_funcs(arg,
_level=_level,
_axis=_axis), None
_axis=_axis), True
else:
result = None

Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,46 @@ def test_multi_function_flexible_mix(df):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = grouped.aggregate(d)
tm.assert_frame_equal(result, expected)


def test_not_as_index_agg_list():

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number. parameterize on as_index=[True,False]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like

@pytest.mark.parametize('as_index', [True, False]); def test_agg_with_as_index(as_index): .... 
# GH 25011
# expected behavhior of agg with as_index=True
as_index = False
array = [[3, 1, 2],
[3, 3, 4],
[4, 5, 6],
[4, 7, 8]]
df = pd.DataFrame(array, columns=['shouldnt_be_index', 'A', 'B'])
groupby = df.groupby('shouldnt_be_index', as_index=as_index)
result = groupby.agg(['min', 'max'])

array2 = [[3, 1, 3, 2, 4],
[4, 5, 7, 6, 8]]
columns = pd.MultiIndex(levels=[['A', 'B', 'shouldnt_be_index'],
['min', 'max', '']],
codes=[[2, 0, 0, 1, 1], [2, 0, 1, 0, 1]])
expected = pd.DataFrame(array2, columns=columns)

tm.assert_frame_equal(result, expected)

# expected behavhior of agg with as_index=True
as_index = True
array = [[3, 1, 2],
[3, 3, 4],
[4, 5, 6],
[4, 7, 8]]
df = pd.DataFrame(array, columns=['should_be_index', 'A', 'B'])
groupby = df.groupby('should_be_index', as_index=as_index)
result = groupby.agg(['min', 'max'])

array2 = [[1, 3, 2, 4],
[5, 7, 6, 8]]
columns = pd.MultiIndex(levels=[['A', 'B'],
['min', 'max']],
codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
index = pd.Series([3, 4], name='should_be_index')
expected = pd.DataFrame(array2, columns=columns, index=index)

tm.assert_frame_equal(result, expected)