diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index c441244b4415d..349c7852a0541 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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`) @@ -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 ^^^^^^^^^ diff --git a/pandas/core/base.py b/pandas/core/base.py index 9a0a4e3e9ca03..6a959e50f8aeb 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -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 diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index ae8ed8db0aa5d..dba732ae3af60 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -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): + + # 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(