Skip to content

Commit 2eeb95f

Browse files
committed
Revert & more tests
1 parent 6447e9f commit 2eeb95f

File tree

5 files changed

+65
-118
lines changed

5 files changed

+65
-118
lines changed

pandas/core/groupby/generic.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
Union,
2222
cast,
2323
)
24-
import warnings
2524

2625
import numpy as np
2726

@@ -33,7 +32,6 @@
3332
Substitution,
3433
doc,
3534
)
36-
from pandas.util._exceptions import find_stack_level
3735

3836
from pandas.core.dtypes.common import (
3937
ensure_int64,
@@ -64,10 +62,6 @@
6462
import pandas.core.common as com
6563
from pandas.core.frame import DataFrame
6664
from pandas.core.groupby import base
67-
from pandas.core.groupby.base import (
68-
reduction_kernels,
69-
transformation_kernels,
70-
)
7165
from pandas.core.groupby.groupby import (
7266
GroupBy,
7367
GroupByPlot,
@@ -332,14 +326,6 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
332326
kwargs = {}
333327

334328
if isinstance(func, str):
335-
if func not in reduction_kernels and not self._grouper._is_resample:
336-
meth = "transform" if func in transformation_kernels else "apply"
337-
warnings.warn(
338-
f"In the future, using the non-aggregation {func=} will raise a "
339-
f"ValueError, use this function with {type(self).__name__}.{meth}",
340-
category=DeprecationWarning,
341-
stacklevel=find_stack_level(),
342-
)
343329
if maybe_use_numba(engine) and engine is not None:
344330
# Not all agg functions support numba, only propagate numba kwargs
345331
# if user asks for numba, and engine is not None
@@ -1576,19 +1562,6 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
15761562
kwargs["engine"] = engine
15771563
kwargs["engine_kwargs"] = engine_kwargs
15781564

1579-
if (
1580-
isinstance(func, str)
1581-
and func not in reduction_kernels
1582-
and not self._grouper._is_resample
1583-
):
1584-
meth = "transform" if func in transformation_kernels else "apply"
1585-
warnings.warn(
1586-
f"In the future, using the non-aggregation {func=} will raise a "
1587-
f"ValueError, use this function with {type(self).__name__}.{meth}",
1588-
category=DeprecationWarning,
1589-
stacklevel=find_stack_level(),
1590-
)
1591-
15921565
op = GroupByApply(self, func, args=args, kwargs=kwargs)
15931566
result = op.agg()
15941567
if not is_dict_like(func) and result is not None:

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,6 @@ def test_groupby_agg_dict_dup_columns():
486486
tm.assert_frame_equal(result, expected)
487487

488488

489-
@pytest.mark.filterwarnings(
490-
"ignore:In the future, using the non-aggregation func:DeprecationWarning"
491-
)
492489
@pytest.mark.parametrize(
493490
"op",
494491
[
@@ -568,9 +565,7 @@ def test_order_aggregate_multiple_funcs():
568565
# GH 25692
569566
df = DataFrame({"A": [1, 1, 2, 2], "B": [1, 2, 3, 4]})
570567

571-
msg = "using the non-aggregation func='ohlc' will raise"
572-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
573-
res = df.groupby("A").agg(["sum", "max", "mean", "ohlc", "min"])
568+
res = df.groupby("A").agg(["sum", "max", "mean", "ohlc", "min"])
574569
result = res.columns.levels[1]
575570

576571
expected = Index(["sum", "max", "mean", "ohlc", "min"])
@@ -1176,6 +1171,22 @@ def test_with_kwargs(self):
11761171
expected = DataFrame({"<lambda_0>": [13], "<lambda_1>": [30]})
11771172
tm.assert_frame_equal(result, expected)
11781173

1174+
def test_unused_kwargs(self):
1175+
# GH#39169 - Passing kwargs used to have agg pass the entire frame rather
1176+
# than column-by-column
1177+
1178+
# UDF that works on both the entire frame and column-by-column
1179+
func = lambda data, **kwargs: np.sum(np.sum(data))
1180+
1181+
df = DataFrame([[1, 2], [3, 4]])
1182+
expected = DataFrame({0: [1, 3], 1: [2, 4]})
1183+
1184+
result = df.groupby(level=0).agg(func)
1185+
tm.assert_frame_equal(result, expected)
1186+
1187+
result = df.groupby(level=0).agg(func, foo=42)
1188+
tm.assert_frame_equal(result, expected)
1189+
11791190
def test_agg_with_one_lambda(self):
11801191
# GH 25719, write tests for DataFrameGroupby.agg with only one lambda
11811192
df = DataFrame(
@@ -1262,6 +1273,40 @@ def test_agg_multiple_lambda(self):
12621273
)
12631274
tm.assert_frame_equal(result2, expected)
12641275

1276+
def test_multiple_udf_same_name(self):
1277+
# GH#28570
1278+
quant50 = partial(np.percentile, q=50)
1279+
quant70 = partial(np.percentile, q=70)
1280+
1281+
df = DataFrame({"col1": ["a", "a", "b", "b", "b"], "col2": [1, 2, 3, 4, 5]})
1282+
expected = DataFrame(
1283+
[[1.5, 1.7], [4.0, 4.4]],
1284+
index=Index(["a", "b"], name="col1"),
1285+
columns=MultiIndex(
1286+
levels=[["col2"], ["percentile"]],
1287+
codes=[[0, 0], [0, 0]],
1288+
),
1289+
)
1290+
gb = df.groupby("col1")
1291+
result = gb.agg({"col2": [quant50, quant70]})
1292+
tm.assert_frame_equal(result, expected)
1293+
1294+
@pytest.mark.parametrize("use_kwargs", [True, False])
1295+
def test_multiple_udf_with_args(self, use_kwargs):
1296+
# GH#26611
1297+
def func(x, y):
1298+
return x.sum() + y
1299+
1300+
df = DataFrame({"A": [1, 2]})
1301+
expected = DataFrame({"A": [13]})
1302+
gb = df.groupby([0, 0])
1303+
if use_kwargs:
1304+
args, kwargs = (), {"y": 10}
1305+
else:
1306+
args, kwargs = (10,), {}
1307+
result = gb.agg(func, *args, **kwargs)
1308+
tm.assert_frame_equal(result, expected)
1309+
12651310

12661311
def test_pass_args_kwargs_duplicate_columns(tsframe, as_index):
12671312
# go through _aggregate_frame with self.axis == 0 and duplicate columns
@@ -1383,14 +1428,9 @@ def test_nonagg_agg():
13831428
df = DataFrame({"a": [1, 1, 2, 2], "b": [1, 2, 2, 1]})
13841429
g = df.groupby("a")
13851430

1386-
msg = "using the non-aggregation func='cumsum' will raise"
1387-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1388-
result = g.agg(["cumsum"])
1431+
result = g.agg(["cumsum"])
13891432
result.columns = result.columns.droplevel(-1)
1390-
1391-
msg = "using the non-aggregation func='cumsum' will raise"
1392-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1393-
expected = g.agg("cumsum")
1433+
expected = g.agg("cumsum")
13941434

13951435
tm.assert_frame_equal(result, expected)
13961436

@@ -1467,7 +1507,6 @@ def test_groupby_agg_precision(any_real_numeric_dtype):
14671507
levels=[["a"], ["b"]], codes=[[0], [0]], names=["key1", "key2"]
14681508
),
14691509
)
1470-
14711510
result = df.groupby(["key1", "key2"]).agg(lambda x: x)
14721511
tm.assert_frame_equal(result, expected)
14731512

@@ -1672,9 +1711,7 @@ def test_groupby_agg_extension_timedelta_cumsum_with_named_aggregation():
16721711
}
16731712
)
16741713
gb = df.groupby("grps")
1675-
msg = "using the non-aggregation func='cumsum' will raise"
1676-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1677-
result = gb.agg(td=("td", "cumsum"))
1714+
result = gb.agg(td=("td", "cumsum"))
16781715
tm.assert_frame_equal(result, expected)
16791716

16801717

pandas/tests/groupby/aggregate/test_cython.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
bdate_range,
2222
)
2323
import pandas._testing as tm
24-
from pandas.core.groupby.base import reduction_kernels
2524

2625

2726
@pytest.mark.parametrize(
@@ -288,16 +287,8 @@ def test_read_only_buffer_source_agg(agg):
288287
)
289288
df._mgr.arrays[0].flags.writeable = False
290289

291-
if agg in reduction_kernels:
292-
warn = None
293-
msg = ""
294-
else:
295-
warn = DeprecationWarning
296-
msg = f"using the non-aggregation func='{agg}' will raise"
297-
with tm.assert_produces_warning(warn, match=msg):
298-
result = df.groupby(["species"]).agg({"sepal_length": agg})
299-
with tm.assert_produces_warning(warn, match=msg):
300-
expected = df.copy().groupby(["species"]).agg({"sepal_length": agg})
290+
result = df.groupby(["species"]).agg({"sepal_length": agg})
291+
expected = df.copy().groupby(["species"]).agg({"sepal_length": agg})
301292

302293
tm.assert_equal(result, expected)
303294

pandas/tests/groupby/test_groupby.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,11 +2936,9 @@ def test_groupby_dropna_with_nunique_unique():
29362936
df = [[1, 1, 1, "A"], [1, None, 1, "A"], [1, None, 2, "A"], [1, None, 3, "A"]]
29372937
df_dropna = DataFrame(df, columns=["a", "b", "c", "partner"])
29382938

2939-
msg = "using the non-aggregation func='unique' will raise"
2940-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
2941-
result = df_dropna.groupby(["a", "b", "c"], dropna=False).agg(
2942-
{"partner": ["nunique", "unique"]}
2943-
)
2939+
result = df_dropna.groupby(["a", "b", "c"], dropna=False).agg(
2940+
{"partner": ["nunique", "unique"]}
2941+
)
29442942

29452943
index = MultiIndex.from_tuples(
29462944
[(1, 1.0, 1), (1, np.nan, 1), (1, np.nan, 2), (1, np.nan, 3)],

pandas/tests/groupby/test_raises.py

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Series,
1616
)
1717
import pandas._testing as tm
18-
from pandas.core.groupby.base import reduction_kernels
1918
from pandas.tests.groupby import get_groupby_method_args
2019

2120

@@ -85,10 +84,8 @@ def df_with_cat_col():
8584
return df
8685

8786

88-
def _call_and_check(
89-
klass, msg, how, gb, groupby_func, args, warn_msg="", warn_category=FutureWarning
90-
):
91-
warn_klass = None if warn_msg == "" else warn_category
87+
def _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=""):
88+
warn_klass = None if warn_msg == "" else FutureWarning
9289
with tm.assert_produces_warning(warn_klass, match=warn_msg, check_stacklevel=False):
9390
if klass is None:
9491
if how == "method":
@@ -186,23 +183,9 @@ def test_groupby_raises_string(
186183
if groupby_func == "fillna":
187184
kind = "Series" if groupby_series else "DataFrame"
188185
warn_msg = f"{kind}GroupBy.fillna is deprecated"
189-
elif groupby_func not in reduction_kernels and how == "agg":
190-
warn_msg = (
191-
f"In the future, using the non-aggregation func='{groupby_func}' will "
192-
"raise a ValueError"
193-
)
194186
else:
195187
warn_msg = ""
196-
_call_and_check(
197-
klass,
198-
msg,
199-
how,
200-
gb,
201-
groupby_func,
202-
args,
203-
warn_msg,
204-
warn_category=DeprecationWarning,
205-
)
188+
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
206189

207190

208191
@pytest.mark.parametrize("how", ["agg", "transform"])
@@ -304,30 +287,12 @@ def test_groupby_raises_datetime(
304287

305288
if groupby_func in ["any", "all"]:
306289
warn_msg = f"'{groupby_func}' with datetime64 dtypes is deprecated"
307-
warn_category = FutureWarning
308290
elif groupby_func == "fillna":
309291
kind = "Series" if groupby_series else "DataFrame"
310292
warn_msg = f"{kind}GroupBy.fillna is deprecated"
311-
warn_category = FutureWarning
312-
elif groupby_func not in reduction_kernels and how == "agg":
313-
warn_msg = (
314-
f"In the future, using the non-aggregation func='{groupby_func}' will "
315-
"raise a ValueError"
316-
)
317-
warn_category = DeprecationWarning
318293
else:
319294
warn_msg = ""
320-
warn_category = FutureWarning
321-
_call_and_check(
322-
klass,
323-
msg,
324-
how,
325-
gb,
326-
groupby_func,
327-
args,
328-
warn_msg=warn_msg,
329-
warn_category=warn_category,
330-
)
295+
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=warn_msg)
331296

332297

333298
@pytest.mark.parametrize("how", ["agg", "transform"])
@@ -522,19 +487,9 @@ def test_groupby_raises_category(
522487
if groupby_func == "fillna":
523488
kind = "Series" if groupby_series else "DataFrame"
524489
warn_msg = f"{kind}GroupBy.fillna is deprecated"
525-
warn_category = FutureWarning
526-
elif groupby_func not in reduction_kernels and how == "agg":
527-
warn_msg = (
528-
f"In the future, using the non-aggregation func='{groupby_func}' "
529-
"will raise a ValueError"
530-
)
531-
warn_category = DeprecationWarning
532490
else:
533491
warn_msg = ""
534-
warn_category = FutureWarning
535-
_call_and_check(
536-
klass, msg, how, gb, groupby_func, args, warn_msg, warn_category=warn_category
537-
)
492+
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
538493

539494

540495
@pytest.mark.parametrize("how", ["agg", "transform"])
@@ -705,13 +660,6 @@ def test_groupby_raises_category_on_category(
705660
if groupby_func == "fillna":
706661
kind = "Series" if groupby_series else "DataFrame"
707662
warn_msg = f"{kind}GroupBy.fillna is deprecated"
708-
warn_category = FutureWarning
709-
elif groupby_func not in reduction_kernels and how == "agg":
710-
warn_msg = f"using the non-aggregation func='{groupby_func}' will raise"
711-
warn_category = DeprecationWarning
712663
else:
713664
warn_msg = ""
714-
warn_category = FutureWarning
715-
_call_and_check(
716-
klass, msg, how, gb, groupby_func, args, warn_msg, warn_category=warn_category
717-
)
665+
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)

0 commit comments

Comments
 (0)