Skip to content

Commit

Permalink
CLN: Enforce deprecation of passing a dict to SeriesGroupBy.agg (#57757)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored Mar 7, 2024
1 parent c71244a commit fd1188a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <whatsnew_220.silent_downcasting>` (: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`)
Expand Down
20 changes: 3 additions & 17 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy as np
import pytest

from pandas.errors import SpecificationError

import pandas as pd
from pandas import (
CategoricalIndex,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fd1188a

Please sign in to comment.