diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 01c2ed3821d7a..00503766b062f 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -54,7 +54,7 @@ notable_bug_fix1 Deprecations ~~~~~~~~~~~~ - 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`) -- +- 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`) .. --------------------------------------------------------------------------- .. _whatsnew_230.performance: diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index db8949788567b..296a601288f9d 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1831,7 +1831,7 @@ def f(g): message=_apply_groupings_depr.format( type(self).__name__, "apply" ), - category=DeprecationWarning, + category=FutureWarning, stacklevel=find_stack_level(), ) except TypeError: diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 0dd808a0ab296..229595202cccb 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -2913,7 +2913,7 @@ def _apply( new_message = _apply_groupings_depr.format("DataFrameGroupBy", "resample") with rewrite_warning( target_message=target_message, - target_category=DeprecationWarning, + target_category=FutureWarning, new_message=new_message, ): result = grouped.apply(how, *args, include_groups=include_groups, **kwargs) diff --git a/pandas/tests/extension/base/groupby.py b/pandas/tests/extension/base/groupby.py index 414683b02dcba..6947e672f3d44 100644 --- a/pandas/tests/extension/base/groupby.py +++ b/pandas/tests/extension/base/groupby.py @@ -114,11 +114,11 @@ 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, 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) diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 2c3e9c1d5e327..8bb5eb2d5c57a 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -1832,7 +1832,7 @@ def test_unstack_bug(self, future_stack): ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(["state", "exp", "barcode", "v"]).apply(len) unstacked = result.unstack() diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index 35ee6c388d5a8..5904b2f48359e 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -505,7 +505,7 @@ 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): assert ts == grouped.apply(lambda x: x.iloc[0]).iloc[0, 1] ts = df["B"].iloc[2] @@ -513,7 +513,7 @@ 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): assert ts == grouped.apply(lambda x: x.iloc[-1]).iloc[0, 1] diff --git a/pandas/tests/groupby/methods/test_value_counts.py b/pandas/tests/groupby/methods/test_value_counts.py index d8c6c7c3fe50c..476ce1fe1b8cc 100644 --- a/pandas/tests/groupby/methods/test_value_counts.py +++ b/pandas/tests/groupby/methods/test_value_counts.py @@ -337,7 +337,7 @@ def test_against_frame_and_seriesgroupby( ) if frame: # compare against apply with DataFrame value_counts - warn = DeprecationWarning if groupby == "column" else None + warn = FutureWarning if groupby == "column" else None msg = "DataFrameGroupBy.apply operated on the grouping columns" with tm.assert_produces_warning(warn, match=msg): expected = gp.apply( diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index cc736f2bf53ba..8ee38a688a1a0 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -30,7 +30,7 @@ def store(group): groups.append(group) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): df.groupby("index").apply(store) expected_value = DataFrame( {"index": [0] * 10, 0: [1] * 10}, index=pd.RangeIndex(0, 100, 10) @@ -114,7 +114,7 @@ def test_apply_index_date_object(): exp_idx = Index(["2011-05-16", "2011-05-17", "2011-05-18"], name="date") expected = Series(["00:00", "02:00", "02:00"], index=exp_idx) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("date", group_keys=False).apply( lambda x: x["time"][x["value"].idxmax()] ) @@ -226,7 +226,7 @@ def f_constant_df(group): del names[:] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): df.groupby("a", group_keys=False).apply(func) assert names == group_names @@ -246,7 +246,7 @@ def test_group_apply_once_per_group2(capsys): ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): df.groupby("group_by_column", group_keys=False).apply( lambda df: print("function_called") ) @@ -270,9 +270,9 @@ def fast(group): return group.copy() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): fast_df = df.groupby("A", group_keys=False).apply(fast) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): slow_df = df.groupby("A", group_keys=False).apply(slow) tm.assert_frame_equal(fast_df, slow_df) @@ -296,7 +296,7 @@ def test_groupby_apply_identity_maybecopy_index_identical(func): df = DataFrame({"g": [1, 2, 2, 2], "a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("g", group_keys=False).apply(func) tm.assert_frame_equal(result, df) @@ -341,9 +341,9 @@ def test_groupby_as_index_apply(): tm.assert_index_equal(res_not_as, exp) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): res_as_apply = g_as.apply(lambda x: x.head(2)).index - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): res_not_as_apply = g_not_as.apply(lambda x: x.head(2)).index # apply doesn't maintain the original ordering @@ -358,7 +358,7 @@ def test_groupby_as_index_apply(): ind = Index(list("abcde")) df = DataFrame([[1, 2], [2, 3], [1, 4], [1, 5], [2, 6]], index=ind) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): res = df.groupby(0, as_index=False, group_keys=False).apply(lambda x: x).index tm.assert_index_equal(res, ind) @@ -389,17 +389,17 @@ def desc3(group): return result msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grouped.apply(desc) assert result.index.names == ("A", "B", "stat") msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result2 = grouped.apply(desc2) assert result2.index.names == ("A", "B", "stat") msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result3 = grouped.apply(desc3) assert result3.index.names == ("A", "B", None) @@ -431,7 +431,7 @@ def test_apply_series_yield_constant(df): def test_apply_frame_yield_constant(df): # GH13568 msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(["A", "B"]).apply(len) assert isinstance(result, Series) assert result.name is None @@ -444,7 +444,7 @@ def test_apply_frame_yield_constant(df): def test_apply_frame_to_series(df): grouped = df.groupby(["A", "B"]) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grouped.apply(len) expected = grouped.count()["C"] tm.assert_index_equal(result.index, expected.index) @@ -455,7 +455,7 @@ def test_apply_frame_not_as_index_column_name(df): # GH 35964 - path within _wrap_applied_output not hit by a test grouped = df.groupby(["A", "B"], as_index=False) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grouped.apply(len) expected = grouped.count().rename(columns={"C": np.nan}).drop(columns="D") # TODO(GH#34306): Use assert_frame_equal when column name is not np.nan @@ -480,7 +480,7 @@ def trans2(group): ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(trans) exp = df.groupby("A")["C"].apply(trans2) tm.assert_series_equal(result, exp, check_names=False) @@ -511,7 +511,7 @@ def test_apply_chunk_view(group_keys): df = 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("key", group_keys=group_keys).apply(lambda x: x.iloc[:2]) expected = df.take([0, 1, 3, 4, 6, 7]) if group_keys: @@ -534,7 +534,7 @@ def test_apply_no_name_column_conflict(): # it works! #2605 grouped = df.groupby(["name", "name2"]) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): grouped.apply(lambda x: x.sort_values("value", inplace=True)) @@ -553,7 +553,7 @@ def f(group): return group msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("d", group_keys=False).apply(f) expected = df.copy() @@ -579,7 +579,7 @@ def f(group): return group msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("d", group_keys=False).apply(f) expected = df.copy() @@ -619,9 +619,9 @@ def filt2(x): return x[x.category == "c"] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = data.groupby("id_field").apply(filt1) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = data.groupby("id_field").apply(filt2) tm.assert_frame_equal(result, expected) @@ -642,7 +642,7 @@ def test_apply_with_duplicated_non_sorted_axis(test_series): tm.assert_series_equal(result, expected) else: msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("Y", group_keys=False).apply(lambda x: x) # not expecting the order to remain the same for duplicated axis @@ -689,7 +689,7 @@ def f(g): return g msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grouped.apply(f) assert "value3" in result @@ -705,11 +705,11 @@ def test_apply_numeric_coercion_when_datetime(): {"Number": [1, 2], "Date": ["2017-03-02"] * 2, "Str": ["foo", "inf"]} ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby(["Number"]).apply(lambda x: x.iloc[0]) df.Date = pd.to_datetime(df.Date) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(["Number"]).apply(lambda x: x.iloc[0]) tm.assert_series_equal(result["Str"], expected["Str"]) @@ -722,7 +722,7 @@ def get_B(g): return g.iloc[0][["B"]] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(get_B)["B"] expected = df.B expected.index = df.A @@ -749,9 +749,9 @@ def predictions(tool): df2 = df1.copy() df2.oTime = pd.to_datetime(df2.oTime) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df1.groupby("Key").apply(predictions).p1 - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df2.groupby("Key").apply(predictions).p1 tm.assert_series_equal(expected, result) @@ -768,7 +768,7 @@ def test_apply_aggregating_timedelta_and_datetime(): ) df["time_delta_zero"] = df.datetime - df.datetime msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("clientid").apply( lambda ddf: Series( {"clientid_age": ddf.time_delta_zero.min(), "date": ddf.datetime.min()} @@ -817,13 +817,13 @@ def func_with_date(batch): return Series({"b": datetime(2015, 1, 1), "c": 2}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): dfg_no_conversion = df.groupby(by=["a"]).apply(func_with_no_date) dfg_no_conversion_expected = DataFrame({"c": 2}, index=[1]) dfg_no_conversion_expected.index.name = "a" msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): dfg_conversion = df.groupby(by=["a"]).apply(func_with_date) dfg_conversion_expected = DataFrame( {"b": pd.Timestamp(2015, 1, 1).as_unit("ns"), "c": 2}, index=[1] @@ -869,7 +869,7 @@ def test_func(x): pass msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = test_df.groupby("groups").apply(test_func) expected = DataFrame() tm.assert_frame_equal(result, expected) @@ -886,9 +886,9 @@ def test_func(x): return x.iloc[[0, -1]] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result1 = test_df1.groupby("groups").apply(test_func) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result2 = test_df2.groupby("groups").apply(test_func) index1 = MultiIndex.from_arrays([[1, 1], [0, 2]], names=["groups", None]) index2 = MultiIndex.from_arrays([[2, 2], [1, 3]], names=["groups", None]) @@ -903,7 +903,7 @@ def test_groupby_apply_return_empty_chunk(): df = DataFrame({"value": [0, 1], "group": ["filled", "empty"]}) groups = df.groupby("group") msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = groups.apply(lambda group: group[group.value != 1]["value"]) expected = Series( [0], @@ -932,7 +932,7 @@ def test_func_returns_object(): # GH 28652 df = DataFrame({"a": [1, 2]}, index=Index([1, 2])) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("a").apply(lambda g: g.index) expected = Series([Index([1]), Index([2])], index=Index([1, 2], name="a")) @@ -951,7 +951,7 @@ def test_apply_datetime_issue(group_column_dtlike): df = DataFrame({"a": ["foo"], "b": [group_column_dtlike]}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("a").apply(lambda x: Series(["spam"], index=[42])) expected = DataFrame(["spam"], Index(["foo"], dtype="str", name="a"), columns=[42]) @@ -990,7 +990,7 @@ def most_common_values(df): return Series({c: s.value_counts().index[0] for c, s in df.items()}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = tdf.groupby("day").apply(most_common_values)["userId"] expected = Series( ["17661101"], index=pd.DatetimeIndex(["2015-02-24"], name="day"), name="userId" @@ -1033,7 +1033,7 @@ def test_groupby_apply_datetime_result_dtypes(using_infer_string): columns=["observation", "color", "mood", "intensity", "score"], ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = data.groupby("color").apply(lambda g: g.iloc[0]).dtypes dtype = pd.StringDtype(na_value=np.nan) if using_infer_string else object expected = Series( @@ -1056,7 +1056,7 @@ def test_apply_index_has_complex_internals(index): # GH 31248 df = DataFrame({"group": [1, 1, 2], "value": [0, 1, 0]}, index=index) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("group", group_keys=False).apply(lambda x: x) tm.assert_frame_equal(result, df) @@ -1081,7 +1081,7 @@ def test_apply_function_returns_non_pandas_non_scalar(function, expected_values) # GH 31441 df = DataFrame(["A", "A", "B", "B"], columns=["groups"]) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("groups").apply(function) expected = Series(expected_values, index=Index(["A", "B"], name="groups")) tm.assert_series_equal(result, expected) @@ -1095,7 +1095,7 @@ def fct(group): df = DataFrame({"A": ["a", "a", "b", "none"], "B": [1, 2, 3, np.nan]}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(fct) expected = Series( [[1.0, 2.0], [3.0], [np.nan]], index=Index(["a", "b", "none"], name="A") @@ -1108,7 +1108,7 @@ def test_apply_function_index_return(function): # GH: 22541 df = DataFrame([1, 2, 2, 2, 1, 2, 3, 1, 3, 1], columns=["id"]) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("id").apply(function) expected = Series( [Index([0, 4, 7, 9]), Index([1, 2, 3, 5]), Index([6, 8])], @@ -1146,7 +1146,7 @@ def test_apply_result_type(group_keys, udf): # regardless of whether the UDF happens to be a transform. df = DataFrame({"A": ["a", "b"], "B": [1, 2]}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): df_result = df.groupby("A", group_keys=group_keys).apply(udf) series_result = df.B.groupby(df.A, group_keys=group_keys).apply(udf) @@ -1163,9 +1163,9 @@ def test_result_order_group_keys_false(): # apply result order should not depend on whether index is the same or just equal df = DataFrame({"A": [2, 1, 2], "B": [1, 2, 3]}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A", group_keys=False).apply(lambda x: x) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby("A", group_keys=False).apply(lambda x: x.copy()) tm.assert_frame_equal(result, expected) @@ -1179,11 +1179,11 @@ def test_apply_with_timezones_aware(): df2 = DataFrame({"x": list(range(2)) * 3, "y": range(6), "t": index_tz}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result1 = df1.groupby("x", group_keys=False).apply( lambda df: df[["x", "y"]].copy() ) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result2 = df2.groupby("x", group_keys=False).apply( lambda df: df[["x", "y"]].copy() ) @@ -1242,7 +1242,7 @@ def test_apply_with_date_in_multiindex_does_not_convert_to_timestamp(): grp = df.groupby(["A", "B"]) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grp.apply(lambda x: x.head(1)) expected = df.iloc[[0, 2, 3]] @@ -1292,7 +1292,7 @@ def test_apply_dropna_with_indexed_same(dropna): index=list("xxyxz"), ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("group", dropna=dropna, group_keys=False).apply(lambda x: x) expected = df.dropna() if dropna else df.iloc[[0, 3, 1, 2, 4]] tm.assert_frame_equal(result, expected) @@ -1320,7 +1320,7 @@ def test_apply_as_index_constant_lambda(as_index, expected): # GH 13217 df = DataFrame({"a": [1, 1, 2, 2], "b": [1, 1, 2, 2], "c": [1, 1, 1, 1]}) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(["a", "b"], as_index=as_index).apply(lambda x: 1) tm.assert_equal(result, expected) @@ -1332,7 +1332,7 @@ def test_sort_index_groups(): index=range(5), ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("C").apply(lambda x: x.A.sort_index()) expected = Series( range(1, 6), @@ -1354,7 +1354,7 @@ def test_positional_slice_groups_datetimelike(): } ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = expected.groupby( [expected.let, expected.date.dt.date], group_keys=False ).apply(lambda x: x.iloc[0:]) @@ -1401,9 +1401,9 @@ def test_apply_na(dropna): ) dfgrp = df.groupby("grp", dropna=dropna) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = dfgrp.apply(lambda grp_df: grp_df.nlargest(1, "z")) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = dfgrp.apply(lambda x: x.sort_values("z", ascending=False).head(1)) tm.assert_frame_equal(result, expected) @@ -1411,7 +1411,7 @@ def test_apply_na(dropna): def test_apply_empty_string_nan_coerce_bug(): # GH#24903 msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = ( DataFrame( { @@ -1448,7 +1448,7 @@ def test_apply_index_key_error_bug(index_values): index=Index(["a2", "a3", "aa"], name="a"), ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = result.groupby("a").apply( lambda df: Series([df["b"].mean()], index=["b_mean"]) ) @@ -1500,7 +1500,7 @@ def test_apply_nonmonotonic_float_index(arg, idx): # GH 34455 expected = DataFrame({"col": arg}, index=idx) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = expected.groupby("col", group_keys=False).apply(lambda x: x) tm.assert_frame_equal(result, expected) @@ -1553,7 +1553,7 @@ def test_include_groups(include_groups): # GH#7155 df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]}) gb = df.groupby("a") - warn = DeprecationWarning if include_groups else None + warn = FutureWarning if include_groups else None msg = "DataFrameGroupBy.apply operated on the grouping columns" with tm.assert_produces_warning(warn, match=msg): result = gb.apply(lambda x: x.sum(), include_groups=include_groups) @@ -1589,11 +1589,11 @@ def test_builtins_apply(keys, f): npfunc = lambda x: getattr(np, fname)(x, axis=0) # numpy's equivalent function msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = gb.apply(npfunc) tm.assert_frame_equal(result, expected) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected2 = gb.apply(lambda x: npfunc(x)) tm.assert_frame_equal(result, expected2) diff --git a/pandas/tests/groupby/test_apply_mutate.py b/pandas/tests/groupby/test_apply_mutate.py index cfd1a4bca9d91..130a29abf9443 100644 --- a/pandas/tests/groupby/test_apply_mutate.py +++ b/pandas/tests/groupby/test_apply_mutate.py @@ -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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): grp_by_copy = df.groupby(["age"], group_keys=False).apply( lambda group: group.copy() ) @@ -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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): grpby_copy = df.groupby("cat1").apply(f_copy) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): grpby_no_copy = df.groupby("cat1").apply(f_no_copy) tm.assert_series_equal(grpby_copy, grpby_no_copy) @@ -68,9 +68,9 @@ 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result1 = df.groupby("key", group_keys=True).apply(lambda x: x[:].key) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result2 = df.groupby("key", group_keys=True).apply(lambda x: x.key) tm.assert_series_equal(result1, result2) @@ -87,7 +87,7 @@ def fn(x): msg = "DataFrameGroupBy.apply operated on the grouping columns" with tm.assert_produces_warning( - DeprecationWarning, match=msg, raise_on_extra_warnings=not warn_copy_on_write + FutureWarning, match=msg, raise_on_extra_warnings=not warn_copy_on_write ): result = df.groupby(["col1"], as_index=False).apply(fn) expected = pd.Series( diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index c70995de7b3b2..cded7a71458fa 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -127,7 +127,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = g.apply(f) expected = x.iloc[[0, 1]].copy() expected.index = Index([1, 2], name="person_id") @@ -335,7 +335,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grouped.apply(lambda x: 1) tm.assert_series_equal(result, expected) @@ -2053,7 +2053,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 = DeprecationWarning if method == "apply" and index_kind == "range" else None + warn = FutureWarning 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)) diff --git a/pandas/tests/groupby/test_counting.py b/pandas/tests/groupby/test_counting.py index 2622895f9f8d2..16d7fe61b90ad 100644 --- a/pandas/tests/groupby/test_counting.py +++ b/pandas/tests/groupby/test_counting.py @@ -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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): right = df.groupby(key).apply(DataFrame.count).drop(key, axis=1) tm.assert_frame_equal(left, right) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 586ef8a126536..57e691b3c508d 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -166,7 +166,7 @@ def max_value(group): return group.loc[group["value"].idxmax()] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): applied = df.groupby("A").apply(max_value) result = applied.dtypes expected = df.dtypes @@ -189,7 +189,7 @@ def f_0(grp): expected = df.groupby("A").first()[["B"]] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(f_0)[["B"]] tm.assert_frame_equal(result, expected) @@ -199,7 +199,7 @@ def f_1(grp): return grp.iloc[0] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(f_1)[["B"]] e = expected.copy() e.loc["Tiger"] = np.nan @@ -211,7 +211,7 @@ def f_2(grp): return grp.iloc[0] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(f_2)[["B"]] e = expected.copy() e.loc["Pony"] = np.nan @@ -224,7 +224,7 @@ def f_3(grp): return grp.iloc[0] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(f_3)[["C"]] e = df.groupby("A").first()[["C"]] e.loc["Pony"] = pd.NaT @@ -237,7 +237,7 @@ def f_4(grp): return grp.iloc[0].loc["C"] msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").apply(f_4) e = df.groupby("A").first()["C"].copy() e.loc["Pony"] = np.nan @@ -424,9 +424,9 @@ def f3(x): # correct result msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result1 = df.groupby("a").apply(f1) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result2 = df2.groupby("a").apply(f1) tm.assert_frame_equal(result1, result2) @@ -1379,13 +1379,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): metrics = df.groupby("A").apply(summarize) assert metrics.columns.name is None - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): metrics = df.groupby("A").apply(summarize, "metrics") assert metrics.columns.name == "metrics" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): metrics = df.groupby("A").apply(summarize_random_name) assert metrics.columns.name is None @@ -1681,7 +1681,7 @@ def test_dont_clobber_name_column(): ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("key", group_keys=False).apply(lambda x: x) tm.assert_frame_equal(result, df) @@ -1769,7 +1769,7 @@ def freducex(x): # make sure all these work msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): grouped.apply(f) grouped.aggregate(freduce) grouped.aggregate({"C": freduce, "D": freduce}) @@ -1792,7 +1792,7 @@ def f(group): return group.copy() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): df.groupby("a", sort=False, group_keys=False).apply(f) expected_names = [0, 1, 2] @@ -2000,7 +2000,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): g.apply(test_sort) @@ -2187,7 +2187,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): res = gb.apply(lambda x: x) assert (res.dtypes == df.dtypes).all() diff --git a/pandas/tests/groupby/test_groupby_dropna.py b/pandas/tests/groupby/test_groupby_dropna.py index 9c01e017dd29c..7e65e56abc4c9 100644 --- a/pandas/tests/groupby/test_groupby_dropna.py +++ b/pandas/tests/groupby/test_groupby_dropna.py @@ -328,7 +328,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = gb.apply(lambda grp: pd.DataFrame({"values": range(len(grp))})) mi_tuples = tuple(zip(data["groups"], selected_data["values"])) diff --git a/pandas/tests/groupby/test_groupby_subclass.py b/pandas/tests/groupby/test_groupby_subclass.py index 0832b67b38098..1a2acb658ee26 100644 --- a/pandas/tests/groupby/test_groupby_subclass.py +++ b/pandas/tests/groupby/test_groupby_subclass.py @@ -74,7 +74,7 @@ def func(group): msg = "DataFrameGroupBy.apply operated on the grouping columns" with tm.assert_produces_warning( - DeprecationWarning, + FutureWarning, match=msg, raise_on_extra_warnings=False, check_stacklevel=False, @@ -126,7 +126,7 @@ 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( - DeprecationWarning, + FutureWarning, match=msg, raise_on_extra_warnings=False, check_stacklevel=False, diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 6e3ae2f7d8fae..7c0a4b78a123d 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -240,7 +240,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = g.apply(lambda x: x.sum()) expected["A"] = [0, 2, 4] expected = expected.loc[:, ["A", "B"]] diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index 69542b934e65f..92dfe146bbb54 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -481,10 +481,10 @@ def sumfunc_series(x): return Series([x["value"].sum()], ("sum",)) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby(Grouper(key="date")).apply(sumfunc_series) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, 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) @@ -502,9 +502,9 @@ def sumfunc_value(x): return x.value.sum() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby(Grouper(key="date")).apply(sumfunc_value) - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, 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) @@ -932,7 +932,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): res = gb.apply(lambda x: x["Quantity"] * 2) dti = Index([Timestamp("2013-12-31")], dtype=df["Date"].dtype, name="Date") diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 395036dd400e5..690eb6f410798 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -683,7 +683,7 @@ def f(group): grouped = df.groupby("c") msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = grouped.apply(f) assert result["d"].dtype == np.float64 @@ -841,7 +841,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 = DeprecationWarning + warn = FutureWarning msg = "DataFrameGroupBy.apply operated on the grouping columns" with tm.assert_produces_warning(warn, match=msg): expected = gb.apply(targop) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index ddd81ab1d347d..80583f5d3c5f2 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -1080,10 +1080,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("ID").resample("5min").sum() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby("ID").apply(lambda x: x.resample("5min").sum()) tm.assert_frame_equal(result, expected) @@ -1104,7 +1104,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("group").resample("1D").ffill() assert result.val.dtype == np.int32 @@ -1881,10 +1881,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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, 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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby("A").resample("D").mean().multiply(multiplier) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index 12abd1c98784b..af4cf5d4ebae5 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -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(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("group").apply(lambda x: x.resample("1D").ffill())[["val"]] tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 32567b4300152..e2d456fea2b23 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -3,8 +3,6 @@ import numpy as np import pytest -from pandas._config import using_string_dtype - from pandas.compat import is_platform_windows import pandas as pd @@ -72,10 +70,10 @@ def f_0(x): return x.set_index("date").resample("D").asfreq() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby("id").apply(f_0) msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.set_index("date").groupby("id").resample("D").asfreq() tm.assert_frame_equal(result, expected) @@ -91,10 +89,10 @@ def f_1(x): return x.resample("1D").ffill() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = df.groupby("group").apply(f_1) msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("group").resample("1D").ffill() tm.assert_frame_equal(result, expected) @@ -111,7 +109,7 @@ def test_getitem(test_frame): tm.assert_series_equal(result, expected) msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = g.resample("2s").mean().B tm.assert_series_equal(result, expected) @@ -237,10 +235,10 @@ def test_methods(f, test_frame): r = g.resample("2s") msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = getattr(r, f)() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: getattr(x.resample("2s"), f)()) tm.assert_equal(result, expected) @@ -259,10 +257,10 @@ def test_methods_std_var(f, test_frame): g = test_frame.groupby("A") r = g.resample("2s") msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = getattr(r, f)(ddof=1) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1)) tm.assert_frame_equal(result, expected) @@ -273,14 +271,14 @@ def test_apply(test_frame): # reduction msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.resample("2s").sum() def f_0(x): return x.resample("2s").sum() msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = r.apply(f_0) tm.assert_frame_equal(result, expected) @@ -288,7 +286,7 @@ def f_1(x): return x.resample("2s").apply(lambda y: y.sum()) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = g.apply(f_1) # y.sum() results in int64 instead of int32 on 32-bit architectures expected = expected.astype("int64") @@ -358,7 +356,7 @@ def test_resample_groupby_with_label(unit): index = date_range("2000-01-01", freq="2D", periods=5, unit=unit) df = DataFrame(index=index, data={"col0": [0, 0, 1, 1, 2], "col1": [1, 1, 1, 1, 1]}) msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("col0").resample("1W", label="left").sum() mi = [ @@ -381,7 +379,7 @@ def test_consistency_with_window(test_frame): df = test_frame expected = Index([1, 2, 3], name="A") msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").resample("2s").mean() assert result.index.nlevels == 2 tm.assert_index_equal(result.index.levels[0], expected) @@ -481,7 +479,7 @@ def test_empty(keys): # GH 26411 df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([])) msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() expected = ( DataFrame(columns=["a", "b"]) @@ -494,7 +492,6 @@ def test_empty(keys): tm.assert_frame_equal(result, expected) -@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)") @pytest.mark.parametrize("consolidate", [True, False]) def test_resample_groupby_agg_object_dtype_all_nan(consolidate): # https://github.com/pandas-dev/pandas/issues/39329 @@ -507,7 +504,7 @@ def test_resample_groupby_agg_object_dtype_all_nan(consolidate): df = df._consolidate() msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(["key"]).resample("W", on="date").min() idx = pd.MultiIndex.from_arrays( [ @@ -559,7 +556,7 @@ def test_resample_no_index(keys): df["date"] = pd.to_datetime(df["date"]) df = df.set_index("date") msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) expected["date"] = pd.to_datetime(expected["date"]) @@ -608,7 +605,7 @@ def test_groupby_resample_size_all_index_same(): index=date_range("31/12/2000 18:00", freq="h", periods=12), ) msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").resample("D").size() mi_exp = pd.MultiIndex.from_arrays( diff --git a/pandas/tests/resample/test_time_grouper.py b/pandas/tests/resample/test_time_grouper.py index 3f9340b800eae..3d9098917a12d 100644 --- a/pandas/tests/resample/test_time_grouper.py +++ b/pandas/tests/resample/test_time_grouper.py @@ -346,7 +346,7 @@ def test_groupby_resample_interpolate(): df["week_starting"] = date_range("01/01/2018", periods=3, freq="W") msg = "DataFrameGroupBy.resample operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = ( df.set_index("week_starting") .groupby("volume") diff --git a/pandas/tests/window/test_groupby.py b/pandas/tests/window/test_groupby.py index 45e7e07affd75..400bf10817ab8 100644 --- a/pandas/tests/window/test_groupby.py +++ b/pandas/tests/window/test_groupby.py @@ -101,7 +101,7 @@ def test_rolling(self, f, roll_frame): result = getattr(r, f)() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: getattr(x.rolling(4), f)()) # groupby.apply doesn't drop the grouped-by column expected = expected.drop("A", axis=1) @@ -117,7 +117,7 @@ def test_rolling_ddof(self, f, roll_frame): result = getattr(r, f)(ddof=1) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1)) # groupby.apply doesn't drop the grouped-by column expected = expected.drop("A", axis=1) @@ -135,7 +135,7 @@ def test_rolling_quantile(self, interpolation, roll_frame): result = r.quantile(0.4, interpolation=interpolation) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply( lambda x: x.rolling(4).quantile(0.4, interpolation=interpolation) ) @@ -182,7 +182,7 @@ def func(x): return getattr(x.rolling(4), f)(roll_frame) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(func) # GH 39591: The grouped column should be all np.nan # (groupby.apply inserts 0s for cov) @@ -200,7 +200,7 @@ def func(x): return getattr(x.B.rolling(4), f)(pairwise=True) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(func) tm.assert_series_equal(result, expected) @@ -247,7 +247,7 @@ def test_rolling_apply(self, raw, roll_frame): # reduction result = r.apply(lambda x: x.sum(), raw=raw) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: x.rolling(4).apply(lambda y: y.sum(), raw=raw)) # groupby.apply doesn't drop the grouped-by column expected = expected.drop("A", axis=1) @@ -793,11 +793,11 @@ def test_groupby_rolling_object_doesnt_affect_groupby_apply(self, roll_frame): # GH 39732 g = roll_frame.groupby("A", group_keys=False) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: x.rolling(4).sum()).index _ = g.rolling(window=4) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): result = g.apply(lambda x: x.rolling(4).sum()).index tm.assert_index_equal(result, expected) @@ -975,7 +975,7 @@ def test_groupby_monotonic(self): df = df.sort_values("date") msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = ( df.set_index("date") .groupby("name") @@ -1000,7 +1000,7 @@ def test_datelike_on_monotonic_within_each_group(self): ) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = ( df.set_index("B") .groupby("A") @@ -1036,7 +1036,7 @@ def test_expanding(self, f, frame): result = getattr(r, f)() msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: getattr(x.expanding(), f)()) # groupby.apply doesn't drop the grouped-by column expected = expected.drop("A", axis=1) @@ -1052,7 +1052,7 @@ def test_expanding_ddof(self, f, frame): result = getattr(r, f)(ddof=0) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0)) # groupby.apply doesn't drop the grouped-by column expected = expected.drop("A", axis=1) @@ -1070,7 +1070,7 @@ def test_expanding_quantile(self, interpolation, frame): result = r.quantile(0.4, interpolation=interpolation) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply( lambda x: x.expanding().quantile(0.4, interpolation=interpolation) ) @@ -1092,7 +1092,7 @@ def func_0(x): return getattr(x.expanding(), f)(frame) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(func_0) # GH 39591: groupby.apply returns 1 instead of nan for windows # with all nan values @@ -1109,7 +1109,7 @@ def func_1(x): return getattr(x.B.expanding(), f)(pairwise=True) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply(func_1) tm.assert_series_equal(result, expected) @@ -1120,7 +1120,7 @@ def test_expanding_apply(self, raw, frame): # reduction result = r.apply(lambda x: x.sum(), raw=raw) msg = "DataFrameGroupBy.apply operated on the grouping columns" - with tm.assert_produces_warning(DeprecationWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): expected = g.apply( lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw) )