Skip to content

Commit a876678

Browse files
rhshadrachpmhatre1
authored andcommitted
CLN: Enforce deprecation of passing a dict to SeriesGroupBy.agg (pandas-dev#57757)
1 parent 3f8750e commit a876678

File tree

4 files changed

+12
-26
lines changed

4 files changed

+12
-26
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Removal of prior version deprecations/changes
201201
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
202202
- Enforced deprecation disallowing parsing datetimes with mixed time zones unless user passes ``utc=True`` to :func:`to_datetime` (:issue:`57275`)
203203
- 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`)
204+
- Enforced deprecation of passing a dictionary to :meth:`SeriesGroupBy.agg` (:issue:`52268`)
204205
- Enforced silent-downcasting deprecation for :ref:`all relevant methods <whatsnew_220.silent_downcasting>` (:issue:`54710`)
205206
- In :meth:`DataFrame.stack`, the default value of ``future_stack`` is now ``True``; specifying ``False`` will raise a ``FutureWarning`` (:issue:`55448`)
206207
- 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`)

pandas/core/groupby/generic.py

+3-17
Original file line numberDiff line numberDiff line change
@@ -384,23 +384,9 @@ def _python_agg_general(self, func, *args, **kwargs):
384384

385385
def _aggregate_multiple_funcs(self, arg, *args, **kwargs) -> DataFrame:
386386
if isinstance(arg, dict):
387-
if self.as_index:
388-
# GH 15931
389-
raise SpecificationError("nested renamer is not supported")
390-
else:
391-
# GH#50684 - This accidentally worked in 1.x
392-
msg = (
393-
"Passing a dictionary to SeriesGroupBy.agg is deprecated "
394-
"and will raise in a future version of pandas. Pass a list "
395-
"of aggregations instead."
396-
)
397-
warnings.warn(
398-
message=msg,
399-
category=FutureWarning,
400-
stacklevel=find_stack_level(),
401-
)
402-
arg = list(arg.items())
403-
elif any(isinstance(x, (tuple, list)) for x in arg):
387+
raise SpecificationError("nested renamer is not supported")
388+
389+
if any(isinstance(x, (tuple, list)) for x in arg):
404390
arg = [(x, x) if not isinstance(x, (tuple, list)) else x for x in arg]
405391
else:
406392
# list of functions / function names

pandas/tests/groupby/aggregate/test_aggregate.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,9 @@ def test_groupby_as_index_agg(df):
10151015

10161016
expected3 = grouped["C"].sum()
10171017
expected3 = DataFrame(expected3).rename(columns={"C": "Q"})
1018-
msg = "Passing a dictionary to SeriesGroupBy.agg is deprecated"
1019-
with tm.assert_produces_warning(FutureWarning, match=msg):
1020-
result3 = grouped["C"].agg({"Q": "sum"})
1021-
tm.assert_frame_equal(result3, expected3)
1018+
msg = "nested renamer is not supported"
1019+
with pytest.raises(SpecificationError, match=msg):
1020+
grouped["C"].agg({"Q": "sum"})
10221021

10231022
# GH7115 & GH8112 & GH8582
10241023
df = DataFrame(

pandas/tests/groupby/test_grouping.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import numpy as np
1010
import pytest
1111

12+
from pandas.errors import SpecificationError
13+
1214
import pandas as pd
1315
from pandas import (
1416
CategoricalIndex,
@@ -530,12 +532,10 @@ def test_multiindex_negative_level(self, multiindex_dataframe_random_data):
530532
).sum()
531533
tm.assert_frame_equal(result, expected)
532534

533-
def test_multifunc_select_col_integer_cols(self, df):
535+
def test_agg_with_dict_raises(self, df):
534536
df.columns = np.arange(len(df.columns))
535-
536-
# it works!
537-
msg = "Passing a dictionary to SeriesGroupBy.agg is deprecated"
538-
with tm.assert_produces_warning(FutureWarning, match=msg):
537+
msg = "nested renamer is not supported"
538+
with pytest.raises(SpecificationError, match=msg):
539539
df.groupby(1, as_index=False)[2].agg({"Q": np.mean})
540540

541541
def test_multiindex_columns_empty_level(self):

0 commit comments

Comments
 (0)