Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: Make FutureWarning into DeprecationWarning for groupby.apply #56952

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ def f(g):
message=_apply_groupings_depr.format(
type(self).__name__, "apply"
),
category=FutureWarning,
category=DeprecationWarning,
stacklevel=find_stack_level(),
)
except TypeError:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2929,7 +2929,7 @@ def _apply(
new_message = _apply_groupings_depr.format("DataFrameGroupBy", "resample")
with rewrite_warning(
target_message=target_message,
target_category=FutureWarning,
target_category=DeprecationWarning,
new_message=new_message,
):
result = grouped.apply(how, *args, include_groups=include_groups, **kwargs)
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/extension/base/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def test_groupby_extension_transform(self, data_for_grouping):
def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op):
df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping})
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.groupby("B", group_keys=False).apply(groupby_apply_op)
df.groupby("B", group_keys=False).A.apply(groupby_apply_op)
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.groupby("B", group_keys=False, observed=False).apply(groupby_apply_op)
df.groupby("B", group_keys=False, observed=False).A.apply(groupby_apply_op)
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.groupby("A", group_keys=False).apply(groupby_apply_op)
df.groupby("A", group_keys=False).B.apply(groupby_apply_op)
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.groupby("A", group_keys=False, observed=False).apply(groupby_apply_op)
df.groupby("A", group_keys=False, observed=False).B.apply(groupby_apply_op)

def test_groupby_apply_identity(self, data_for_grouping):
df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping})
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ def test_unstack_bug(self, future_stack):
)

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

unstacked = result.unstack()
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/groupby/aggregate/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,15 @@ def test_agg_timezone_round_trip():

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

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

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


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/methods/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def test_against_frame_and_seriesgroupby(
)
if frame:
# compare against apply with DataFrame value_counts
warn = FutureWarning if groupby == "column" else None
warn = DeprecationWarning if groupby == "column" else None
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(warn, match=msg):
expected = gp.apply(
Expand Down
142 changes: 71 additions & 71 deletions pandas/tests/groupby/test_apply.py

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions pandas/tests/groupby/test_apply_mutate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def test_group_by_copy():
).set_index("name")

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
grp_by_same_value = df.groupby(["age"], group_keys=False).apply(
lambda group: group
)
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
grp_by_copy = df.groupby(["age"], group_keys=False).apply(
lambda group: group.copy()
)
Expand Down Expand Up @@ -54,9 +54,9 @@ def f_no_copy(x):
return x.groupby("cat2")["rank"].min()

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
grpby_copy = df.groupby("cat1").apply(f_copy)
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
grpby_no_copy = df.groupby("cat1").apply(f_no_copy)
tm.assert_series_equal(grpby_copy, grpby_no_copy)

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

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result1 = df.groupby("key", group_keys=True).apply(lambda x: x[:].key)
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result2 = df.groupby("key", group_keys=True).apply(lambda x: x.key)
tm.assert_series_equal(result1, result2)


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

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(
DeprecationWarning, match=msg, raise_on_extra_warnings=not warn_copy_on_write
):
result = df.groupby(["col1"], as_index=False).apply(fn)
expected = pd.Series(
[1, 2, 0, 4, 5, 0],
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def f(x):
return x.drop_duplicates("person_name").iloc[0]

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = g.apply(f)
expected = x.iloc[[0, 1]].copy()
expected.index = Index([1, 2], name="person_id")
Expand Down Expand Up @@ -333,7 +333,7 @@ def test_apply(ordered):
idx = MultiIndex.from_arrays([missing, dense], names=["missing", "dense"])
expected = Series(1, index=idx)
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = grouped.apply(lambda x: 1)
tm.assert_series_equal(result, expected)

Expand Down Expand Up @@ -2049,7 +2049,7 @@ def test_category_order_apply(as_index, sort, observed, method, index_kind, orde
df["a2"] = df["a"]
df = df.set_index(keys)
gb = df.groupby(keys, as_index=as_index, sort=sort, observed=observed)
warn = FutureWarning if method == "apply" and index_kind == "range" else None
warn = DeprecationWarning if method == "apply" and index_kind == "range" else None
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(warn, match=msg):
op_result = getattr(gb, method)(lambda x: x.sum(numeric_only=True))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def test_count():
for key in ["1st", "2nd", ["1st", "2nd"]]:
left = df.groupby(key).count()
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
right = df.groupby(key).apply(DataFrame.count).drop(key, axis=1)
tm.assert_frame_equal(left, right)

Expand Down
22 changes: 11 additions & 11 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def max_value(group):
return group.loc[group["value"].idxmax()]

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

# correct result
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result1 = df.groupby("a").apply(f1)
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result2 = df2.groupby("a").apply(f1)
tm.assert_frame_equal(result1, result2)

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

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
metrics = df.groupby("A").apply(summarize)
assert metrics.columns.name is None
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
metrics = df.groupby("A").apply(summarize, "metrics")
assert metrics.columns.name == "metrics"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
metrics = df.groupby("A").apply(summarize_random_name)
assert metrics.columns.name is None

Expand Down Expand Up @@ -1393,7 +1393,7 @@ def test_dont_clobber_name_column():
)

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

Expand Down Expand Up @@ -1477,7 +1477,7 @@ def freducex(x):

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

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

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

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
g.apply(test_sort)


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

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_groupby_dropna.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def test_groupby_apply_with_dropna_for_multi_index(dropna, data, selected_data,
df = pd.DataFrame(data)
gb = df.groupby("groups", dropna=dropna)
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = gb.apply(lambda grp: pd.DataFrame({"values": range(len(grp))}))

mi_tuples = tuple(zip(data["groups"], selected_data["values"]))
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/groupby/test_groupby_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def func(group):

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(
FutureWarning, match=msg, raise_on_extra_warnings=False
DeprecationWarning,
match=msg,
raise_on_extra_warnings=False,
check_stacklevel=False,
):
result = custom_df.groupby("c").apply(func)
expected = tm.SubclassedSeries(["hello"] * 3, index=Index([7, 8, 9], name="c"))
Expand Down Expand Up @@ -123,7 +126,10 @@ def test_groupby_resample_preserves_subclass(obj):
# Confirm groupby.resample() preserves dataframe type
msg = "DataFrameGroupBy.resample operated on the grouping columns"
with tm.assert_produces_warning(
FutureWarning, match=msg, raise_on_extra_warnings=False
DeprecationWarning,
match=msg,
raise_on_extra_warnings=False,
check_stacklevel=False,
):
result = df.groupby("Buyer").resample("5D").sum()
assert isinstance(result, obj)
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def test_grouper_creation_bug(self):
tm.assert_frame_equal(result, expected)

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = g.apply(lambda x: x.sum())
expected["A"] = [0, 2, 4]
expected = expected.loc[:, ["A", "B"]]
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,10 @@ def sumfunc_series(x):
return Series([x["value"].sum()], ("sum",))

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
expected = df.groupby(Grouper(key="date")).apply(sumfunc_series)
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_series)
tm.assert_frame_equal(
result.reset_index(drop=True), expected.reset_index(drop=True)
Expand All @@ -499,9 +499,9 @@ def sumfunc_value(x):
return x.value.sum()

msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
expected = df.groupby(Grouper(key="date")).apply(sumfunc_value)
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_value)
tm.assert_series_equal(
result.reset_index(drop=True), expected.reset_index(drop=True)
Expand Down Expand Up @@ -929,7 +929,7 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(

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

dti = Index([Timestamp("2013-12-31")], dtype=df["Date"].dtype, name="Date")
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def f(group):

grouped = df.groupby("c")
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = grouped.apply(f)

assert result["d"].dtype == np.float64
Expand Down Expand Up @@ -825,7 +825,7 @@ def test_cython_transform_frame(request, op, args, targop, df_fix, gb_target):
if op != "shift" or not isinstance(gb_target.get("by"), (str, list)):
warn = None
else:
warn = FutureWarning
warn = DeprecationWarning
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(warn, match=msg):
expected = gb.apply(targop)
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,10 +1058,10 @@ def test_resample_segfault(unit):
).set_index("timestamp")
df.index = df.index.as_unit(unit)
msg = "DataFrameGroupBy.resample operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = df.groupby("ID").resample("5min").sum()
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
expected = df.groupby("ID").apply(lambda x: x.resample("5min").sum())
tm.assert_frame_equal(result, expected)

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

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

Expand Down Expand Up @@ -1863,10 +1863,10 @@ def f(data, add_arg):

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_groupby_resample_api():
index = pd.MultiIndex.from_arrays([[1] * 8 + [2] * 8, i], names=["group", "date"])
expected = DataFrame({"val": [5] * 7 + [6] + [7] * 7 + [8]}, index=index)
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = df.groupby("group").apply(lambda x: x.resample("1D").ffill())[["val"]]
tm.assert_frame_equal(result, expected)

Expand Down
Loading