diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index e68a935fe6fd3..4f690e9339f6b 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -201,6 +201,7 @@ Removal of prior version deprecations/changes - Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`) - Enforced deprecation disallowing parsing datetimes with mixed time zones unless user passes ``utc=True`` to :func:`to_datetime` (:issue:`57275`) - Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`) +- Enforced deprecation of passing a dictionary to :meth:`SeriesGroupBy.agg` (:issue:`52268`) - Enforced silent-downcasting deprecation for :ref:`all relevant methods ` (:issue:`54710`) - In :meth:`DataFrame.stack`, the default value of ``future_stack`` is now ``True``; specifying ``False`` will raise a ``FutureWarning`` (:issue:`55448`) - Methods ``apply``, ``agg``, and ``transform`` will no longer replace NumPy functions (e.g. ``np.sum``) and built-in functions (e.g. ``min``) with the equivalent pandas implementation; use string aliases (e.g. ``"sum"`` and ``"min"``) if you desire to use the pandas implementation (:issue:`53974`) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 52fd7735b533e..fc2fc366e18db 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -384,23 +384,9 @@ def _python_agg_general(self, func, *args, **kwargs): def _aggregate_multiple_funcs(self, arg, *args, **kwargs) -> DataFrame: if isinstance(arg, dict): - if self.as_index: - # GH 15931 - raise SpecificationError("nested renamer is not supported") - else: - # GH#50684 - This accidentally worked in 1.x - msg = ( - "Passing a dictionary to SeriesGroupBy.agg is deprecated " - "and will raise in a future version of pandas. Pass a list " - "of aggregations instead." - ) - warnings.warn( - message=msg, - category=FutureWarning, - stacklevel=find_stack_level(), - ) - arg = list(arg.items()) - elif any(isinstance(x, (tuple, list)) for x in arg): + raise SpecificationError("nested renamer is not supported") + + if any(isinstance(x, (tuple, list)) for x in arg): arg = [(x, x) if not isinstance(x, (tuple, list)) else x for x in arg] else: # list of functions / function names diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 5d44f11393c93..d8f832002dac6 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1015,10 +1015,9 @@ def test_groupby_as_index_agg(df): expected3 = grouped["C"].sum() expected3 = DataFrame(expected3).rename(columns={"C": "Q"}) - msg = "Passing a dictionary to SeriesGroupBy.agg is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - result3 = grouped["C"].agg({"Q": "sum"}) - tm.assert_frame_equal(result3, expected3) + msg = "nested renamer is not supported" + with pytest.raises(SpecificationError, match=msg): + grouped["C"].agg({"Q": "sum"}) # GH7115 & GH8112 & GH8582 df = DataFrame( diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 2961369936717..04a3516fd9af7 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -9,6 +9,8 @@ import numpy as np import pytest +from pandas.errors import SpecificationError + import pandas as pd from pandas import ( CategoricalIndex, @@ -530,12 +532,10 @@ def test_multiindex_negative_level(self, multiindex_dataframe_random_data): ).sum() tm.assert_frame_equal(result, expected) - def test_multifunc_select_col_integer_cols(self, df): + def test_agg_with_dict_raises(self, df): df.columns = np.arange(len(df.columns)) - - # it works! - msg = "Passing a dictionary to SeriesGroupBy.agg is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + msg = "nested renamer is not supported" + with pytest.raises(SpecificationError, match=msg): df.groupby(1, as_index=False)[2].agg({"Q": np.mean}) def test_multiindex_columns_empty_level(self):