Skip to content

Commit 52be621

Browse files
rhshadrachjorisvandenbosschelithomas1
authored andcommitted
DEPR: Make FutureWarning into DeprecationWarning for groupby.apply (pandas-dev#56952)
* DEPR: Make FutureWarning into DeprecationWarning for groupby.apply * suppress extra warning when CoW warning mode --------- Co-authored-by: Joris Van den Bossche <[email protected]> Co-authored-by: Thomas Li <[email protected]>
1 parent 462b775 commit 52be621

21 files changed

+167
-158
lines changed

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,7 @@ def f(g):
18561856
message=_apply_groupings_depr.format(
18571857
type(self).__name__, "apply"
18581858
),
1859-
category=FutureWarning,
1859+
category=DeprecationWarning,
18601860
stacklevel=find_stack_level(),
18611861
)
18621862
except TypeError:

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2929,7 +2929,7 @@ def _apply(
29292929
new_message = _apply_groupings_depr.format("DataFrameGroupBy", "resample")
29302930
with rewrite_warning(
29312931
target_message=target_message,
2932-
target_category=FutureWarning,
2932+
target_category=DeprecationWarning,
29332933
new_message=new_message,
29342934
):
29352935
result = grouped.apply(how, *args, include_groups=include_groups, **kwargs)

pandas/tests/extension/base/groupby.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ 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(FutureWarning, match=msg):
118-
df.groupby("B", group_keys=False).apply(groupby_apply_op)
119-
df.groupby("B", group_keys=False).A.apply(groupby_apply_op)
117+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
118+
df.groupby("B", group_keys=False, observed=False).apply(groupby_apply_op)
119+
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(FutureWarning, match=msg):
122-
df.groupby("A", group_keys=False).apply(groupby_apply_op)
123-
df.groupby("A", group_keys=False).B.apply(groupby_apply_op)
121+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
122+
df.groupby("A", group_keys=False, observed=False).apply(groupby_apply_op)
123+
df.groupby("A", group_keys=False, observed=False).B.apply(groupby_apply_op)
124124

125125
def test_groupby_apply_identity(self, data_for_grouping):
126126
df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping})

pandas/tests/frame/test_stack_unstack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,7 @@ def test_unstack_bug(self, future_stack):
18171817
)
18181818

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

18231823
unstacked = result.unstack()

pandas/tests/groupby/aggregate/test_other.py

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

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

508508
ts = df["B"].iloc[2]
509509
assert ts == grouped.last()["B"].iloc[0]
510510

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

516516

pandas/tests/groupby/methods/test_value_counts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def test_against_frame_and_seriesgroupby(
326326
)
327327
if frame:
328328
# compare against apply with DataFrame value_counts
329-
warn = FutureWarning if groupby == "column" else None
329+
warn = DeprecationWarning if groupby == "column" else None
330330
msg = "DataFrameGroupBy.apply operated on the grouping columns"
331331
with tm.assert_produces_warning(warn, match=msg):
332332
expected = gp.apply(

pandas/tests/groupby/test_apply.py

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

pandas/tests/groupby/test_apply_mutate.py

+10-8
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(FutureWarning, match=msg):
17+
with tm.assert_produces_warning(DeprecationWarning, 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(FutureWarning, match=msg):
22+
with tm.assert_produces_warning(DeprecationWarning, 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(FutureWarning, match=msg):
57+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
5858
grpby_copy = df.groupby("cat1").apply(f_copy)
59-
with tm.assert_produces_warning(FutureWarning, match=msg):
59+
with tm.assert_produces_warning(DeprecationWarning, 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,14 +68,14 @@ 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(FutureWarning, match=msg):
71+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
7272
result1 = df.groupby("key", group_keys=True).apply(lambda x: x[:].key)
73-
with tm.assert_produces_warning(FutureWarning, match=msg):
73+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
7474
result2 = df.groupby("key", group_keys=True).apply(lambda x: x.key)
7575
tm.assert_series_equal(result1, result2)
7676

7777

78-
def test_apply_function_with_indexing():
78+
def test_apply_function_with_indexing(warn_copy_on_write):
7979
# GH: 33058
8080
df = pd.DataFrame(
8181
{"col1": ["A", "A", "A", "B", "B", "B"], "col2": [1, 2, 3, 4, 5, 6]}
@@ -86,7 +86,9 @@ def fn(x):
8686
return x.col2
8787

8888
msg = "DataFrameGroupBy.apply operated on the grouping columns"
89-
with tm.assert_produces_warning(FutureWarning, match=msg):
89+
with tm.assert_produces_warning(
90+
DeprecationWarning, match=msg, raise_on_extra_warnings=not warn_copy_on_write
91+
):
9092
result = df.groupby(["col1"], as_index=False).apply(fn)
9193
expected = pd.Series(
9294
[1, 2, 0, 4, 5, 0],

pandas/tests/groupby/test_categorical.py

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

127127
msg = "DataFrameGroupBy.apply operated on the grouping columns"
128-
with tm.assert_produces_warning(FutureWarning, match=msg):
128+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
129129
result = g.apply(f)
130130
expected = x.iloc[[0, 1]].copy()
131131
expected.index = Index([1, 2], name="person_id")
@@ -333,7 +333,7 @@ def test_apply(ordered):
333333
idx = MultiIndex.from_arrays([missing, dense], names=["missing", "dense"])
334334
expected = Series(1, index=idx)
335335
msg = "DataFrameGroupBy.apply operated on the grouping columns"
336-
with tm.assert_produces_warning(FutureWarning, match=msg):
336+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
337337
result = grouped.apply(lambda x: 1)
338338
tm.assert_series_equal(result, expected)
339339

@@ -2049,7 +2049,7 @@ def test_category_order_apply(as_index, sort, observed, method, index_kind, orde
20492049
df["a2"] = df["a"]
20502050
df = df.set_index(keys)
20512051
gb = df.groupby(keys, as_index=as_index, sort=sort, observed=observed)
2052-
warn = FutureWarning if method == "apply" and index_kind == "range" else None
2052+
warn = DeprecationWarning if method == "apply" and index_kind == "range" else None
20532053
msg = "DataFrameGroupBy.apply operated on the grouping columns"
20542054
with tm.assert_produces_warning(warn, match=msg):
20552055
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(FutureWarning, match=msg):
293+
with tm.assert_produces_warning(DeprecationWarning, 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

+11-11
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def max_value(group):
7070
return group.loc[group["value"].idxmax()]
7171

7272
msg = "DataFrameGroupBy.apply operated on the grouping columns"
73-
with tm.assert_produces_warning(FutureWarning, match=msg):
73+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
7474
applied = df.groupby("A").apply(max_value)
7575
result = applied.dtypes
7676
expected = df.dtypes
@@ -203,9 +203,9 @@ def f3(x):
203203

204204
# correct result
205205
msg = "DataFrameGroupBy.apply operated on the grouping columns"
206-
with tm.assert_produces_warning(FutureWarning, match=msg):
206+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
207207
result1 = df.groupby("a").apply(f1)
208-
with tm.assert_produces_warning(FutureWarning, match=msg):
208+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
209209
result2 = df2.groupby("a").apply(f1)
210210
tm.assert_frame_equal(result1, result2)
211211

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

10941094
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1095-
with tm.assert_produces_warning(FutureWarning, match=msg):
1095+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
10961096
metrics = df.groupby("A").apply(summarize)
10971097
assert metrics.columns.name is None
1098-
with tm.assert_produces_warning(FutureWarning, match=msg):
1098+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
10991099
metrics = df.groupby("A").apply(summarize, "metrics")
11001100
assert metrics.columns.name == "metrics"
1101-
with tm.assert_produces_warning(FutureWarning, match=msg):
1101+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
11021102
metrics = df.groupby("A").apply(summarize_random_name)
11031103
assert metrics.columns.name is None
11041104

@@ -1393,7 +1393,7 @@ def test_dont_clobber_name_column():
13931393
)
13941394

13951395
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1396-
with tm.assert_produces_warning(FutureWarning, match=msg):
1396+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
13971397
result = df.groupby("key", group_keys=False).apply(lambda x: x)
13981398
tm.assert_frame_equal(result, df)
13991399

@@ -1477,7 +1477,7 @@ def freducex(x):
14771477

14781478
# make sure all these work
14791479
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1480-
with tm.assert_produces_warning(FutureWarning, match=msg):
1480+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
14811481
grouped.apply(f)
14821482
grouped.aggregate(freduce)
14831483
grouped.aggregate({"C": freduce, "D": freduce})
@@ -1500,7 +1500,7 @@ def f(group):
15001500
return group.copy()
15011501

15021502
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1503-
with tm.assert_produces_warning(FutureWarning, match=msg):
1503+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
15041504
df.groupby("a", sort=False, group_keys=False).apply(f)
15051505

15061506
expected_names = [0, 1, 2]
@@ -1708,7 +1708,7 @@ def test_sort(x):
17081708
tm.assert_frame_equal(x, x.sort_values(by=sort_column))
17091709

17101710
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1711-
with tm.assert_produces_warning(FutureWarning, match=msg):
1711+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
17121712
g.apply(test_sort)
17131713

17141714

@@ -1893,7 +1893,7 @@ def test_empty_groupby_apply_nonunique_columns():
18931893
df.columns = [0, 1, 2, 0]
18941894
gb = df.groupby(df[1], group_keys=False)
18951895
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1896-
with tm.assert_produces_warning(FutureWarning, match=msg):
1896+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
18971897
res = gb.apply(lambda x: x)
18981898
assert (res.dtypes == df.dtypes).all()
18991899

pandas/tests/groupby/test_groupby_dropna.py

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

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

pandas/tests/groupby/test_groupby_subclass.py

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

7575
msg = "DataFrameGroupBy.apply operated on the grouping columns"
7676
with tm.assert_produces_warning(
77-
FutureWarning, match=msg, raise_on_extra_warnings=False
77+
DeprecationWarning,
78+
match=msg,
79+
raise_on_extra_warnings=False,
80+
check_stacklevel=False,
7881
):
7982
result = custom_df.groupby("c").apply(func)
8083
expected = tm.SubclassedSeries(["hello"] * 3, index=Index([7, 8, 9], name="c"))
@@ -123,7 +126,10 @@ def test_groupby_resample_preserves_subclass(obj):
123126
# Confirm groupby.resample() preserves dataframe type
124127
msg = "DataFrameGroupBy.resample operated on the grouping columns"
125128
with tm.assert_produces_warning(
126-
FutureWarning, match=msg, raise_on_extra_warnings=False
129+
DeprecationWarning,
130+
match=msg,
131+
raise_on_extra_warnings=False,
132+
check_stacklevel=False,
127133
):
128134
result = df.groupby("Buyer").resample("5D").sum()
129135
assert isinstance(result, obj)

pandas/tests/groupby/test_grouping.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def test_grouper_creation_bug(self):
238238
tm.assert_frame_equal(result, expected)
239239

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

pandas/tests/groupby/test_timegrouper.py

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

480480
msg = "DataFrameGroupBy.apply operated on the grouping columns"
481-
with tm.assert_produces_warning(FutureWarning, match=msg):
481+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
482482
expected = df.groupby(Grouper(key="date")).apply(sumfunc_series)
483483
msg = "DataFrameGroupBy.apply operated on the grouping columns"
484-
with tm.assert_produces_warning(FutureWarning, match=msg):
484+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
485485
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_series)
486486
tm.assert_frame_equal(
487487
result.reset_index(drop=True), expected.reset_index(drop=True)
@@ -499,9 +499,9 @@ def sumfunc_value(x):
499499
return x.value.sum()
500500

501501
msg = "DataFrameGroupBy.apply operated on the grouping columns"
502-
with tm.assert_produces_warning(FutureWarning, match=msg):
502+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
503503
expected = df.groupby(Grouper(key="date")).apply(sumfunc_value)
504-
with tm.assert_produces_warning(FutureWarning, match=msg):
504+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
505505
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_value)
506506
tm.assert_series_equal(
507507
result.reset_index(drop=True), expected.reset_index(drop=True)
@@ -929,7 +929,7 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
929929

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

935935
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
@@ -668,7 +668,7 @@ def f(group):
668668

669669
grouped = df.groupby("c")
670670
msg = "DataFrameGroupBy.apply operated on the grouping columns"
671-
with tm.assert_produces_warning(FutureWarning, match=msg):
671+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
672672
result = grouped.apply(f)
673673

674674
assert result["d"].dtype == np.float64
@@ -825,7 +825,7 @@ def test_cython_transform_frame(request, op, args, targop, df_fix, gb_target):
825825
if op != "shift" or not isinstance(gb_target.get("by"), (str, list)):
826826
warn = None
827827
else:
828-
warn = FutureWarning
828+
warn = DeprecationWarning
829829
msg = "DataFrameGroupBy.apply operated on the grouping columns"
830830
with tm.assert_produces_warning(warn, match=msg):
831831
expected = gb.apply(targop)

pandas/tests/resample/test_datetime_index.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,10 @@ def test_resample_segfault(unit):
10581058
).set_index("timestamp")
10591059
df.index = df.index.as_unit(unit)
10601060
msg = "DataFrameGroupBy.resample operated on the grouping columns"
1061-
with tm.assert_produces_warning(FutureWarning, match=msg):
1061+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
10621062
result = df.groupby("ID").resample("5min").sum()
10631063
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1064-
with tm.assert_produces_warning(FutureWarning, match=msg):
1064+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
10651065
expected = df.groupby("ID").apply(lambda x: x.resample("5min").sum())
10661066
tm.assert_frame_equal(result, expected)
10671067

@@ -1082,7 +1082,7 @@ def test_resample_dtype_preservation(unit):
10821082
assert result.val.dtype == np.int32
10831083

10841084
msg = "DataFrameGroupBy.resample operated on the grouping columns"
1085-
with tm.assert_produces_warning(FutureWarning, match=msg):
1085+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
10861086
result = df.groupby("group").resample("1D").ffill()
10871087
assert result.val.dtype == np.int32
10881088

@@ -1863,10 +1863,10 @@ def f(data, add_arg):
18631863

18641864
df = DataFrame({"A": 1, "B": 2}, index=date_range("2017", periods=10))
18651865
msg = "DataFrameGroupBy.resample operated on the grouping columns"
1866-
with tm.assert_produces_warning(FutureWarning, match=msg):
1866+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
18671867
result = df.groupby("A").resample("D").agg(f, multiplier).astype(float)
18681868
msg = "DataFrameGroupBy.resample operated on the grouping columns"
1869-
with tm.assert_produces_warning(FutureWarning, match=msg):
1869+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
18701870
expected = df.groupby("A").resample("D").mean().multiply(multiplier)
18711871
tm.assert_frame_equal(result, expected)
18721872

pandas/tests/resample/test_resample_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_groupby_resample_api():
7878
index = pd.MultiIndex.from_arrays([[1] * 8 + [2] * 8, i], names=["group", "date"])
7979
expected = DataFrame({"val": [5] * 7 + [6] + [7] * 7 + [8]}, index=index)
8080
msg = "DataFrameGroupBy.apply operated on the grouping columns"
81-
with tm.assert_produces_warning(FutureWarning, match=msg):
81+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
8282
result = df.groupby("group").apply(lambda x: x.resample("1D").ffill())[["val"]]
8383
tm.assert_frame_equal(result, expected)
8484

0 commit comments

Comments
 (0)