Skip to content

BUG: Using agg with groupy, as_index=False still returning group variable as index #25114

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

Closed
wants to merge 13 commits into from
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 @@ -375,7 +375,6 @@ Plotting
Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^

- Bug in :meth:`pandas.core.resample.Resampler.agg` with a timezone aware index where ``OverflowError`` would raise when passing a list of functions (:issue:`22660`)
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`)
- Bug in :func:`pandas.core.groupby.GroupBy.agg` when applying a aggregation function to timezone aware data (:issue:`23683`)
- Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` where timezone information would be dropped (:issue:`21603`)
Expand All @@ -384,6 +383,7 @@ Groupby/Resample/Rolling
- Ensured that result group order is correct when grouping on an ordered ``Categorical`` and specifying ``observed=True`` (:issue:`25871`, :issue:`25167`)
- Bug in :meth:`pandas.core.window.Rolling.min` and :meth:`pandas.core.window.Rolling.max` that caused a memory leak (:issue:`25893`)
- Bug in :func:`idxmax` and :func:`idxmin` on :meth:`DataFrame.groupby` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`)
- Bug in ::meth:`pandas.core.groupby.GroupBy.aggwhen passingas_index=False``, returning an additional column.

Reshaping
^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,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
34 changes: 34 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,40 @@ def test_multi_function_flexible_mix(df):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize('as_index', [True, False])
def test_not_as_index_agg_list(as_index):

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
array = [[3, 1, 2],
[3, 3, 4],
[4, 5, 6],
[4, 7, 8]]
df = pd.DataFrame(array, columns=['index_iff_as_index', 'A', 'B'])
groupby = df.groupby('index_iff_as_index', as_index=as_index)
result = groupby.agg(['min', 'max'])

if as_index:

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='index_iff_as_index')
expected = pd.DataFrame(array2, columns=columns, index=index)

else:

array2 = [[3, 1, 3, 2, 4],
[4, 5, 7, 6, 8]]
columns = pd.MultiIndex(levels=[['A', 'B', 'index_iff_as_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)


def test_groupby_agg_coercing_bools():
# issue 14873
dat = pd.DataFrame(
Expand Down