Skip to content

Commit a298795

Browse files
authored
DEPR: Update groupby.apply DeprecationWarning to FutureWarning (#59751)
* DEPR: Update groupby.apply DeprecationWarning to FutureWarning * Remove xfail * Add whatsnew note
1 parent 84a37f7 commit a298795

22 files changed

+151
-154
lines changed

doc/source/whatsnew/v2.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ notable_bug_fix1
5454
Deprecations
5555
~~~~~~~~~~~~
5656
- Deprecated allowing non-``bool`` values for ``na`` in :meth:`.str.contains`, :meth:`.str.startswith`, and :meth:`.str.endswith` for dtypes that do not already disallow these (:issue:`59615`)
57-
-
57+
- The deprecation of setting the argument ``include_groups`` to ``True`` in :meth:`DataFrameGroupBy.apply` has been promoted from a ``DeprecationWarning`` to ``FutureWarning``; only ``False`` will be allowed (:issue:`7155`)
5858

5959
.. ---------------------------------------------------------------------------
6060
.. _whatsnew_230.performance:

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,7 @@ def f(g):
18311831
message=_apply_groupings_depr.format(
18321832
type(self).__name__, "apply"
18331833
),
1834-
category=DeprecationWarning,
1834+
category=FutureWarning,
18351835
stacklevel=find_stack_level(),
18361836
)
18371837
except TypeError:

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2913,7 +2913,7 @@ def _apply(
29132913
new_message = _apply_groupings_depr.format("DataFrameGroupBy", "resample")
29142914
with rewrite_warning(
29152915
target_message=target_message,
2916-
target_category=DeprecationWarning,
2916+
target_category=FutureWarning,
29172917
new_message=new_message,
29182918
):
29192919
result = grouped.apply(how, *args, include_groups=include_groups, **kwargs)

pandas/tests/extension/base/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ def test_groupby_extension_transform(self, data_for_grouping):
114114
def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op):
115115
df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping})
116116
msg = "DataFrameGroupBy.apply operated on the grouping columns"
117-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
117+
with tm.assert_produces_warning(FutureWarning, match=msg):
118118
df.groupby("B", group_keys=False, observed=False).apply(groupby_apply_op)
119119
df.groupby("B", group_keys=False, observed=False).A.apply(groupby_apply_op)
120120
msg = "DataFrameGroupBy.apply operated on the grouping columns"
121-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
121+
with tm.assert_produces_warning(FutureWarning, match=msg):
122122
df.groupby("A", group_keys=False, observed=False).apply(groupby_apply_op)
123123
df.groupby("A", group_keys=False, observed=False).B.apply(groupby_apply_op)
124124

pandas/tests/frame/test_stack_unstack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ def test_unstack_bug(self, future_stack):
18321832
)
18331833

18341834
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1835-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1835+
with tm.assert_produces_warning(FutureWarning, match=msg):
18361836
result = df.groupby(["state", "exp", "barcode", "v"]).apply(len)
18371837

18381838
unstacked = result.unstack()

pandas/tests/groupby/aggregate/test_other.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ def test_agg_timezone_round_trip():
505505

506506
# GH#27110 applying iloc should return a DataFrame
507507
msg = "DataFrameGroupBy.apply operated on the grouping columns"
508-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
508+
with tm.assert_produces_warning(FutureWarning, match=msg):
509509
assert ts == grouped.apply(lambda x: x.iloc[0]).iloc[0, 1]
510510

511511
ts = df["B"].iloc[2]
512512
assert ts == grouped.last()["B"].iloc[0]
513513

514514
# GH#27110 applying iloc should return a DataFrame
515515
msg = "DataFrameGroupBy.apply operated on the grouping columns"
516-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
516+
with tm.assert_produces_warning(FutureWarning, match=msg):
517517
assert ts == grouped.apply(lambda x: x.iloc[-1]).iloc[0, 1]
518518

519519

pandas/tests/groupby/methods/test_value_counts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def test_against_frame_and_seriesgroupby(
337337
)
338338
if frame:
339339
# compare against apply with DataFrame value_counts
340-
warn = DeprecationWarning if groupby == "column" else None
340+
warn = FutureWarning if groupby == "column" else None
341341
msg = "DataFrameGroupBy.apply operated on the grouping columns"
342342
with tm.assert_produces_warning(warn, match=msg):
343343
expected = gp.apply(

pandas/tests/groupby/test_apply.py

+63-63
Large diffs are not rendered by default.

pandas/tests/groupby/test_apply_mutate.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def test_group_by_copy():
1414
).set_index("name")
1515

1616
msg = "DataFrameGroupBy.apply operated on the grouping columns"
17-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
17+
with tm.assert_produces_warning(FutureWarning, match=msg):
1818
grp_by_same_value = df.groupby(["age"], group_keys=False).apply(
1919
lambda group: group
2020
)
2121
msg = "DataFrameGroupBy.apply operated on the grouping columns"
22-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
22+
with tm.assert_produces_warning(FutureWarning, match=msg):
2323
grp_by_copy = df.groupby(["age"], group_keys=False).apply(
2424
lambda group: group.copy()
2525
)
@@ -54,9 +54,9 @@ def f_no_copy(x):
5454
return x.groupby("cat2")["rank"].min()
5555

5656
msg = "DataFrameGroupBy.apply operated on the grouping columns"
57-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
57+
with tm.assert_produces_warning(FutureWarning, match=msg):
5858
grpby_copy = df.groupby("cat1").apply(f_copy)
59-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
59+
with tm.assert_produces_warning(FutureWarning, match=msg):
6060
grpby_no_copy = df.groupby("cat1").apply(f_no_copy)
6161
tm.assert_series_equal(grpby_copy, grpby_no_copy)
6262

@@ -68,9 +68,9 @@ def test_no_mutate_but_looks_like():
6868
df = pd.DataFrame({"key": [1, 1, 1, 2, 2, 2, 3, 3, 3], "value": range(9)})
6969

7070
msg = "DataFrameGroupBy.apply operated on the grouping columns"
71-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
71+
with tm.assert_produces_warning(FutureWarning, match=msg):
7272
result1 = df.groupby("key", group_keys=True).apply(lambda x: x[:].key)
73-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
73+
with tm.assert_produces_warning(FutureWarning, match=msg):
7474
result2 = df.groupby("key", group_keys=True).apply(lambda x: x.key)
7575
tm.assert_series_equal(result1, result2)
7676

@@ -87,7 +87,7 @@ def fn(x):
8787

8888
msg = "DataFrameGroupBy.apply operated on the grouping columns"
8989
with tm.assert_produces_warning(
90-
DeprecationWarning, match=msg, raise_on_extra_warnings=not warn_copy_on_write
90+
FutureWarning, match=msg, raise_on_extra_warnings=not warn_copy_on_write
9191
):
9292
result = df.groupby(["col1"], as_index=False).apply(fn)
9393
expected = pd.Series(

pandas/tests/groupby/test_categorical.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def f(x):
127127
return x.drop_duplicates("person_name").iloc[0]
128128

129129
msg = "DataFrameGroupBy.apply operated on the grouping columns"
130-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
130+
with tm.assert_produces_warning(FutureWarning, match=msg):
131131
result = g.apply(f)
132132
expected = x.iloc[[0, 1]].copy()
133133
expected.index = Index([1, 2], name="person_id")
@@ -335,7 +335,7 @@ def test_apply(ordered):
335335
idx = MultiIndex.from_arrays([missing, dense], names=["missing", "dense"])
336336
expected = Series(1, index=idx)
337337
msg = "DataFrameGroupBy.apply operated on the grouping columns"
338-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
338+
with tm.assert_produces_warning(FutureWarning, match=msg):
339339
result = grouped.apply(lambda x: 1)
340340
tm.assert_series_equal(result, expected)
341341

@@ -2053,7 +2053,7 @@ def test_category_order_apply(as_index, sort, observed, method, index_kind, orde
20532053
df["a2"] = df["a"]
20542054
df = df.set_index(keys)
20552055
gb = df.groupby(keys, as_index=as_index, sort=sort, observed=observed)
2056-
warn = DeprecationWarning if method == "apply" and index_kind == "range" else None
2056+
warn = FutureWarning if method == "apply" and index_kind == "range" else None
20572057
msg = "DataFrameGroupBy.apply operated on the grouping columns"
20582058
with tm.assert_produces_warning(warn, match=msg):
20592059
op_result = getattr(gb, method)(lambda x: x.sum(numeric_only=True))

pandas/tests/groupby/test_counting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def test_count():
290290
for key in ["1st", "2nd", ["1st", "2nd"]]:
291291
left = df.groupby(key).count()
292292
msg = "DataFrameGroupBy.apply operated on the grouping columns"
293-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
293+
with tm.assert_produces_warning(FutureWarning, match=msg):
294294
right = df.groupby(key).apply(DataFrame.count).drop(key, axis=1)
295295
tm.assert_frame_equal(left, right)
296296

pandas/tests/groupby/test_groupby.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def max_value(group):
166166
return group.loc[group["value"].idxmax()]
167167

168168
msg = "DataFrameGroupBy.apply operated on the grouping columns"
169-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
169+
with tm.assert_produces_warning(FutureWarning, match=msg):
170170
applied = df.groupby("A").apply(max_value)
171171
result = applied.dtypes
172172
expected = df.dtypes
@@ -189,7 +189,7 @@ def f_0(grp):
189189

190190
expected = df.groupby("A").first()[["B"]]
191191
msg = "DataFrameGroupBy.apply operated on the grouping columns"
192-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
192+
with tm.assert_produces_warning(FutureWarning, match=msg):
193193
result = df.groupby("A").apply(f_0)[["B"]]
194194
tm.assert_frame_equal(result, expected)
195195

@@ -199,7 +199,7 @@ def f_1(grp):
199199
return grp.iloc[0]
200200

201201
msg = "DataFrameGroupBy.apply operated on the grouping columns"
202-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
202+
with tm.assert_produces_warning(FutureWarning, match=msg):
203203
result = df.groupby("A").apply(f_1)[["B"]]
204204
e = expected.copy()
205205
e.loc["Tiger"] = np.nan
@@ -211,7 +211,7 @@ def f_2(grp):
211211
return grp.iloc[0]
212212

213213
msg = "DataFrameGroupBy.apply operated on the grouping columns"
214-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
214+
with tm.assert_produces_warning(FutureWarning, match=msg):
215215
result = df.groupby("A").apply(f_2)[["B"]]
216216
e = expected.copy()
217217
e.loc["Pony"] = np.nan
@@ -224,7 +224,7 @@ def f_3(grp):
224224
return grp.iloc[0]
225225

226226
msg = "DataFrameGroupBy.apply operated on the grouping columns"
227-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
227+
with tm.assert_produces_warning(FutureWarning, match=msg):
228228
result = df.groupby("A").apply(f_3)[["C"]]
229229
e = df.groupby("A").first()[["C"]]
230230
e.loc["Pony"] = pd.NaT
@@ -237,7 +237,7 @@ def f_4(grp):
237237
return grp.iloc[0].loc["C"]
238238

239239
msg = "DataFrameGroupBy.apply operated on the grouping columns"
240-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
240+
with tm.assert_produces_warning(FutureWarning, match=msg):
241241
result = df.groupby("A").apply(f_4)
242242
e = df.groupby("A").first()["C"].copy()
243243
e.loc["Pony"] = np.nan
@@ -424,9 +424,9 @@ def f3(x):
424424

425425
# correct result
426426
msg = "DataFrameGroupBy.apply operated on the grouping columns"
427-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
427+
with tm.assert_produces_warning(FutureWarning, match=msg):
428428
result1 = df.groupby("a").apply(f1)
429-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
429+
with tm.assert_produces_warning(FutureWarning, match=msg):
430430
result2 = df2.groupby("a").apply(f1)
431431
tm.assert_frame_equal(result1, result2)
432432

@@ -1379,13 +1379,13 @@ def summarize_random_name(df):
13791379
return Series({"count": 1, "mean": 2, "omissions": 3}, name=df.iloc[0]["A"])
13801380

13811381
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1382-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1382+
with tm.assert_produces_warning(FutureWarning, match=msg):
13831383
metrics = df.groupby("A").apply(summarize)
13841384
assert metrics.columns.name is None
1385-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1385+
with tm.assert_produces_warning(FutureWarning, match=msg):
13861386
metrics = df.groupby("A").apply(summarize, "metrics")
13871387
assert metrics.columns.name == "metrics"
1388-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1388+
with tm.assert_produces_warning(FutureWarning, match=msg):
13891389
metrics = df.groupby("A").apply(summarize_random_name)
13901390
assert metrics.columns.name is None
13911391

@@ -1681,7 +1681,7 @@ def test_dont_clobber_name_column():
16811681
)
16821682

16831683
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1684-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1684+
with tm.assert_produces_warning(FutureWarning, match=msg):
16851685
result = df.groupby("key", group_keys=False).apply(lambda x: x)
16861686
tm.assert_frame_equal(result, df)
16871687

@@ -1769,7 +1769,7 @@ def freducex(x):
17691769

17701770
# make sure all these work
17711771
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1772-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1772+
with tm.assert_produces_warning(FutureWarning, match=msg):
17731773
grouped.apply(f)
17741774
grouped.aggregate(freduce)
17751775
grouped.aggregate({"C": freduce, "D": freduce})
@@ -1792,7 +1792,7 @@ def f(group):
17921792
return group.copy()
17931793

17941794
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1795-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
1795+
with tm.assert_produces_warning(FutureWarning, match=msg):
17961796
df.groupby("a", sort=False, group_keys=False).apply(f)
17971797

17981798
expected_names = [0, 1, 2]
@@ -2000,7 +2000,7 @@ def test_sort(x):
20002000
tm.assert_frame_equal(x, x.sort_values(by=sort_column))
20012001

20022002
msg = "DataFrameGroupBy.apply operated on the grouping columns"
2003-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
2003+
with tm.assert_produces_warning(FutureWarning, match=msg):
20042004
g.apply(test_sort)
20052005

20062006

@@ -2187,7 +2187,7 @@ def test_empty_groupby_apply_nonunique_columns():
21872187
df.columns = [0, 1, 2, 0]
21882188
gb = df.groupby(df[1], group_keys=False)
21892189
msg = "DataFrameGroupBy.apply operated on the grouping columns"
2190-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
2190+
with tm.assert_produces_warning(FutureWarning, match=msg):
21912191
res = gb.apply(lambda x: x)
21922192
assert (res.dtypes == df.dtypes).all()
21932193

pandas/tests/groupby/test_groupby_dropna.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_groupby_apply_with_dropna_for_multi_index(dropna, data, selected_data,
328328
df = pd.DataFrame(data)
329329
gb = df.groupby("groups", dropna=dropna)
330330
msg = "DataFrameGroupBy.apply operated on the grouping columns"
331-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
331+
with tm.assert_produces_warning(FutureWarning, match=msg):
332332
result = gb.apply(lambda grp: pd.DataFrame({"values": range(len(grp))}))
333333

334334
mi_tuples = tuple(zip(data["groups"], selected_data["values"]))

pandas/tests/groupby/test_groupby_subclass.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def func(group):
7474

7575
msg = "DataFrameGroupBy.apply operated on the grouping columns"
7676
with tm.assert_produces_warning(
77-
DeprecationWarning,
77+
FutureWarning,
7878
match=msg,
7979
raise_on_extra_warnings=False,
8080
check_stacklevel=False,
@@ -126,7 +126,7 @@ def test_groupby_resample_preserves_subclass(obj):
126126
# Confirm groupby.resample() preserves dataframe type
127127
msg = "DataFrameGroupBy.resample operated on the grouping columns"
128128
with tm.assert_produces_warning(
129-
DeprecationWarning,
129+
FutureWarning,
130130
match=msg,
131131
raise_on_extra_warnings=False,
132132
check_stacklevel=False,

pandas/tests/groupby/test_grouping.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def test_grouper_creation_bug(self):
240240
tm.assert_frame_equal(result, expected)
241241

242242
msg = "DataFrameGroupBy.apply operated on the grouping columns"
243-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
243+
with tm.assert_produces_warning(FutureWarning, match=msg):
244244
result = g.apply(lambda x: x.sum())
245245
expected["A"] = [0, 2, 4]
246246
expected = expected.loc[:, ["A", "B"]]

pandas/tests/groupby/test_timegrouper.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,10 @@ def sumfunc_series(x):
481481
return Series([x["value"].sum()], ("sum",))
482482

483483
msg = "DataFrameGroupBy.apply operated on the grouping columns"
484-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
484+
with tm.assert_produces_warning(FutureWarning, match=msg):
485485
expected = df.groupby(Grouper(key="date")).apply(sumfunc_series)
486486
msg = "DataFrameGroupBy.apply operated on the grouping columns"
487-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
487+
with tm.assert_produces_warning(FutureWarning, match=msg):
488488
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_series)
489489
tm.assert_frame_equal(
490490
result.reset_index(drop=True), expected.reset_index(drop=True)
@@ -502,9 +502,9 @@ def sumfunc_value(x):
502502
return x.value.sum()
503503

504504
msg = "DataFrameGroupBy.apply operated on the grouping columns"
505-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
505+
with tm.assert_produces_warning(FutureWarning, match=msg):
506506
expected = df.groupby(Grouper(key="date")).apply(sumfunc_value)
507-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
507+
with tm.assert_produces_warning(FutureWarning, match=msg):
508508
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_value)
509509
tm.assert_series_equal(
510510
result.reset_index(drop=True), expected.reset_index(drop=True)
@@ -932,7 +932,7 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
932932

933933
# function that returns a Series
934934
msg = "DataFrameGroupBy.apply operated on the grouping columns"
935-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
935+
with tm.assert_produces_warning(FutureWarning, match=msg):
936936
res = gb.apply(lambda x: x["Quantity"] * 2)
937937

938938
dti = Index([Timestamp("2013-12-31")], dtype=df["Date"].dtype, name="Date")

pandas/tests/groupby/transform/test_transform.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ def f(group):
683683

684684
grouped = df.groupby("c")
685685
msg = "DataFrameGroupBy.apply operated on the grouping columns"
686-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
686+
with tm.assert_produces_warning(FutureWarning, match=msg):
687687
result = grouped.apply(f)
688688

689689
assert result["d"].dtype == np.float64
@@ -841,7 +841,7 @@ def test_cython_transform_frame(request, op, args, targop, df_fix, gb_target):
841841
if op != "shift" or not isinstance(gb_target.get("by"), (str, list)):
842842
warn = None
843843
else:
844-
warn = DeprecationWarning
844+
warn = FutureWarning
845845
msg = "DataFrameGroupBy.apply operated on the grouping columns"
846846
with tm.assert_produces_warning(warn, match=msg):
847847
expected = gb.apply(targop)

0 commit comments

Comments
 (0)