From c96551cdb8d4cf18269bac5b8808d3e91e98ca21 Mon Sep 17 00:00:00 2001 From: Abdulaziz Aloqeely <52792999+Aloqeely@users.noreply.github.com> Date: Tue, 21 May 2024 18:17:55 +0300 Subject: [PATCH] Use Pandas40DeprecationWarning for new deprecations --- pandas/core/generic.py | 5 +-- pandas/core/indexes/accessors.py | 9 ++-- pandas/core/window/expanding.py | 1 - pandas/core/window/rolling.py | 1 - pandas/tests/extension/test_arrow.py | 5 ++- .../tests/frame/methods/test_reindex_like.py | 6 ++- .../tests/io/json/test_json_table_schema.py | 6 ++- pandas/tests/io/json/test_pandas.py | 43 ++++++++++--------- pandas/tests/io/test_gcs.py | 4 +- pandas/tests/resample/test_datetime_index.py | 3 +- .../series/accessors/test_cat_accessor.py | 4 +- .../series/accessors/test_dt_accessor.py | 3 +- .../tests/series/methods/test_reindex_like.py | 10 +++-- 13 files changed, 58 insertions(+), 42 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f6874a3a3113c..32ca315876efe 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -101,7 +101,6 @@ Pandas40DeprecationWarning, find_stack_level, ) - from pandas.util._validators import ( check_dtype_backend, validate_ascending, @@ -2568,7 +2567,7 @@ def to_json( warnings.warn( "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead.", - FutureWarning, + Pandas40DeprecationWarning, stacklevel=find_stack_level(), ) elif date_format == "epoch": @@ -2576,7 +2575,7 @@ def to_json( warnings.warn( "'epoch' date format is deprecated and will be removed in a future " "version, please use 'iso' date format instead.", - FutureWarning, + Pandas40DeprecationWarning, stacklevel=find_stack_level(), ) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 3cb51f7447677..0e6c85f56ff82 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -14,7 +14,10 @@ import numpy as np from pandas._libs import lib -from pandas.util._exceptions import find_stack_level +from pandas.util._exceptions import ( + Pandas40DeprecationWarning, + find_stack_level, +) from pandas.core.dtypes.common import ( is_integer_dtype, @@ -218,7 +221,7 @@ def to_pytimedelta(self): "in a future version this will return a Series containing python " "datetime.timedelta objects instead of an ndarray. To retain the " "old behavior, call `np.array` on the result", - FutureWarning, + Pandas40DeprecationWarning, stacklevel=find_stack_level(), ) return cast(ArrowExtensionArray, self._parent.array)._dt_to_pytimedelta() @@ -479,7 +482,7 @@ def to_pytimedelta(self) -> np.ndarray: "in a future version this will return a Series containing python " "datetime.timedelta objects instead of an ndarray. To retain the " "old behavior, call `np.array` on the result", - FutureWarning, + Pandas40DeprecationWarning, stacklevel=find_stack_level(), ) return self._get_values().to_pytimedelta() diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 15646b46f86aa..f14954cd9a4b0 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -706,7 +706,6 @@ def kurt(self, numeric_only: bool = False): aggregation_description="quantile", agg_method="quantile", ) - def quantile( self, q: float, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 4a0a39aabd13f..2243d8dd1a613 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -2553,7 +2553,6 @@ def kurt(self, numeric_only: bool = False): aggregation_description="quantile", agg_method="quantile", ) - def quantile( self, q: float, diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 7d31fe6085c3a..d33ebf2574f9f 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -43,6 +43,7 @@ pa_version_under13p0, pa_version_under14p0, ) +from pandas.util._exceptions import Pandas40DeprecationWarning import pandas.util._test_decorators as td from pandas.core.dtypes.dtypes import ( @@ -2862,14 +2863,14 @@ def test_dt_to_pytimedelta(): ser = pd.Series(data, dtype=ArrowDtype(pa.duration("ns"))) msg = "The behavior of ArrowTemporalProperties.to_pytimedelta is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): result = ser.dt.to_pytimedelta() expected = np.array(data, dtype=object) tm.assert_numpy_array_equal(result, expected) assert all(type(res) is timedelta for res in result) msg = "The behavior of TimedeltaProperties.to_pytimedelta is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): expected = ser.astype("timedelta64[ns]").dt.to_pytimedelta() tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/frame/methods/test_reindex_like.py b/pandas/tests/frame/methods/test_reindex_like.py index 03968dcbb6314..3ab6752f8e6b5 100644 --- a/pandas/tests/frame/methods/test_reindex_like.py +++ b/pandas/tests/frame/methods/test_reindex_like.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.util._exceptions import Pandas40DeprecationWarning + from pandas import DataFrame import pandas._testing as tm @@ -22,10 +24,10 @@ def test_reindex_like(self, float_frame): def test_reindex_like_methods(self, method, expected_values): df = DataFrame({"x": list(range(5))}) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): result = df.reindex_like(df, method=method, tolerance=0) tm.assert_frame_equal(df, result) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): result = df.reindex_like(df, method=method, tolerance=[0, 0, 0, 0]) tm.assert_frame_equal(df, result) diff --git a/pandas/tests/io/json/test_json_table_schema.py b/pandas/tests/io/json/test_json_table_schema.py index a0d5b3a741aaf..18c1d6bd395ce 100644 --- a/pandas/tests/io/json/test_json_table_schema.py +++ b/pandas/tests/io/json/test_json_table_schema.py @@ -7,6 +7,8 @@ import numpy as np import pytest +from pandas.util._exceptions import Pandas40DeprecationWarning + from pandas.core.dtypes.dtypes import ( CategoricalDtype, DatetimeTZDtype, @@ -460,7 +462,9 @@ def test_date_format_raises(self, df_table): "version, please use 'iso' date format instead." ) with pytest.raises(ValueError, match=error_msg): - with tm.assert_produces_warning(FutureWarning, match=warning_msg): + with tm.assert_produces_warning( + Pandas40DeprecationWarning, match=warning_msg + ): df_table.to_json(orient="table", date_format="epoch") # others work diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index c4065ea01988f..67f6738e81f4a 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -13,6 +13,7 @@ from pandas._config import using_pyarrow_string_dtype from pandas.compat import IS64 +from pandas.util._exceptions import Pandas40DeprecationWarning import pandas.util._test_decorators as td import pandas as pd @@ -142,7 +143,7 @@ def test_frame_non_unique_columns(self, orient, data): "in a future version, please use 'iso' date format instead." ) if df.iloc[:, 0].dtype == "datetime64[ns]": - expected_warning = FutureWarning + expected_warning = Pandas40DeprecationWarning with tm.assert_produces_warning(expected_warning, match=msg): result = read_json( @@ -774,7 +775,7 @@ def test_series_with_dtype_datetime(self, dtype, expected): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): data = StringIO(s.to_json()) result = read_json(data, typ="series", dtype=dtype) tm.assert_series_equal(result, expected) @@ -825,13 +826,13 @@ def test_convert_dates(self, datetime_series, datetime_frame): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): json = StringIO(df.to_json()) result = read_json(json) tm.assert_frame_equal(result, df) df["foo"] = 1.0 - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): json = StringIO(df.to_json(date_unit="ns")) result = read_json(json, convert_dates=False) @@ -842,7 +843,7 @@ def test_convert_dates(self, datetime_series, datetime_frame): # series ts = Series(Timestamp("20130101").as_unit("ns"), index=datetime_series.index) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): json = StringIO(ts.to_json()) result = read_json(json, typ="series") tm.assert_series_equal(result, ts) @@ -860,7 +861,7 @@ def test_date_index_and_values(self, date_format, as_object, date_typ): expected_warning = None if date_format == "epoch": expected = '{"1577836800000":1577836800000,"null":null}' - expected_warning = FutureWarning + expected_warning = Pandas40DeprecationWarning else: expected = ( '{"2020-01-01T00:00:00.000":"2020-01-01T00:00:00.000","null":null}' @@ -973,7 +974,7 @@ def test_date_unit(self, unit, datetime_frame): "'epoch' date format is deprecated and will be removed in a future " "version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): json = df.to_json(date_format="epoch", date_unit=unit) # force date unit @@ -993,13 +994,13 @@ def test_date_unit(self, unit, datetime_frame): DataFrame( {"A": ["a", "b", "c"], "B": pd.to_timedelta(np.arange(3), unit="D")} ), - FutureWarning, + Pandas40DeprecationWarning, ), ( DataFrame( {"A": pd.to_datetime(["2020-01-01", "2020-02-01", "2020-03-01"])} ), - FutureWarning, + Pandas40DeprecationWarning, ), ], ) @@ -1085,7 +1086,7 @@ def test_doc_example(self): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): json = StringIO(dfj2.to_json()) result = read_json(json, dtype={"ints": np.int64, "bools": np.bool_}) tm.assert_frame_equal(result, result) @@ -1127,20 +1128,20 @@ def test_timedelta(self): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): result = read_json(StringIO(ser.to_json()), typ="series").apply(converter) tm.assert_series_equal(result, ser) ser = Series([timedelta(23), timedelta(seconds=5)], index=Index([0, 1])) assert ser.dtype == "timedelta64[ns]" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): result = read_json(StringIO(ser.to_json()), typ="series").apply(converter) tm.assert_series_equal(result, ser) frame = DataFrame([timedelta(23), timedelta(seconds=5)]) assert frame[0].dtype == "timedelta64[ns]" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): json = frame.to_json() tm.assert_frame_equal(frame, read_json(StringIO(json)).apply(converter)) @@ -1156,7 +1157,7 @@ def test_timedelta2(self): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): data = StringIO(frame.to_json(date_unit="ns")) result = read_json(data) result["a"] = pd.to_timedelta(result.a, unit="ns") @@ -1191,7 +1192,7 @@ def test_timedelta_to_json(self, as_object, date_format, timedelta_typ): '{"P1DT0H0M0S":"P1DT0H0M0S","P2DT0H0M0S":"P2DT0H0M0S","null":null}' ) else: - expected_warning = FutureWarning + expected_warning = Pandas40DeprecationWarning expected = '{"86400000":86400000,"172800000":172800000,"null":null}' if as_object: @@ -1210,7 +1211,7 @@ def test_timedelta_to_json(self, as_object, date_format, timedelta_typ): def test_timedelta_to_json_fractional_precision(self, as_object, timedelta_typ): data = [timedelta_typ(milliseconds=42)] ser = Series(data, index=data) - warn = FutureWarning + warn = Pandas40DeprecationWarning if as_object: ser = ser.astype(object) warn = None @@ -1306,13 +1307,13 @@ def test_datetime_tz(self): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): expected = df_naive.to_json() assert expected == df.to_json() stz = Series(tz_range) s_naive = Series(tz_naive) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): assert stz.to_json() == s_naive.to_json() def test_sparse(self): @@ -1582,7 +1583,7 @@ def test_to_json_from_json_columns_dtypes(self, orient): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): dfjson = expected.to_json(orient=orient) result = read_json( @@ -1776,7 +1777,7 @@ def test_timedelta_as_label(self, date_format, key): expected_warning = None if date_format == "epoch": - expected_warning = FutureWarning + expected_warning = Pandas40DeprecationWarning msg = ( "'epoch' date format is deprecated and will be removed in a future " @@ -2021,7 +2022,7 @@ def test_json_pandas_nulls(self, nulls_fixture, request): "in a future version, please use 'iso' date format instead." ) if nulls_fixture is pd.NaT: - expected_warning = FutureWarning + expected_warning = Pandas40DeprecationWarning with tm.assert_produces_warning(expected_warning, match=msg): result = DataFrame([[nulls_fixture]]).to_json() diff --git a/pandas/tests/io/test_gcs.py b/pandas/tests/io/test_gcs.py index 4b2be41d0c9f9..85212348bc4df 100644 --- a/pandas/tests/io/test_gcs.py +++ b/pandas/tests/io/test_gcs.py @@ -7,6 +7,8 @@ import numpy as np import pytest +from pandas.util._exceptions import Pandas40DeprecationWarning + from pandas import ( DataFrame, Index, @@ -82,7 +84,7 @@ def test_to_read_gcs(gcs_buffer, format, monkeypatch, capsys): "The default 'epoch' date format is deprecated and will be removed " "in a future version, please use 'iso' date format instead." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): df1.to_json(path) df2 = read_json(path, convert_dates=["dt"]) elif format == "parquet": diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index c38d223c9d6a0..387830bc6bb2e 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -8,6 +8,7 @@ from pandas._libs import lib from pandas._typing import DatetimeNaTType from pandas.compat import is_platform_windows +from pandas.util._exceptions import Pandas40DeprecationWarning import pandas.util._test_decorators as td import pandas as pd @@ -1300,7 +1301,7 @@ def test_resample_consistency(unit): s10 = s.reindex(index=i10, method="bfill") s10_2 = s.reindex(index=i10, method="bfill", limit=2) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): rl = s.reindex_like(s10, method="bfill", limit=2) r10_2 = s.resample("10Min").bfill(limit=2) r10 = s.resample("10Min").bfill() diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index ce8ea27ea1fa2..4c9034d645844 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.util._exceptions import Pandas40DeprecationWarning + from pandas import ( Categorical, DataFrame, @@ -202,7 +204,7 @@ def test_dt_accessor_api_for_categorical(self, idx): warn_cls.append(UserWarning) elif func == "to_pytimedelta": # GH 57463 - warn_cls.append(FutureWarning) + warn_cls.append(Pandas40DeprecationWarning) if warn_cls: warn_cls = tuple(warn_cls) else: diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 8c60f7beb317d..074b23e1d5597 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -12,6 +12,7 @@ import pytz from pandas._libs.tslibs.timezones import maybe_get_tz +from pandas.util._exceptions import Pandas40DeprecationWarning from pandas.core.dtypes.common import ( is_integer_dtype, @@ -193,7 +194,7 @@ def test_dt_namespace_accessor_timedelta(self): tm.assert_index_equal(result.index, ser.index) msg = "The behavior of TimedeltaProperties.to_pytimedelta is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas40DeprecationWarning, match=msg): result = ser.dt.to_pytimedelta() assert isinstance(result, np.ndarray) assert result.dtype == object diff --git a/pandas/tests/series/methods/test_reindex_like.py b/pandas/tests/series/methods/test_reindex_like.py index 10b8ac5817636..81311fbe14f18 100644 --- a/pandas/tests/series/methods/test_reindex_like.py +++ b/pandas/tests/series/methods/test_reindex_like.py @@ -2,6 +2,8 @@ import numpy as np +from pandas.util._exceptions import Pandas40DeprecationWarning + from pandas import Series import pandas._testing as tm @@ -20,7 +22,7 @@ def test_reindex_like(datetime_series): series1 = Series([5, None, None], [day1, day2, day3]) series2 = Series([None, None], [day1, day3]) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): result = series1.reindex_like(series2, method="pad") expected = Series([5, np.nan], index=[day1, day3]) tm.assert_series_equal(result, expected) @@ -33,13 +35,13 @@ def test_reindex_like_nearest(): other = ser.reindex(target, method="nearest") expected = Series(np.around(target).astype("int64"), target) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): result = ser.reindex_like(other, method="nearest") tm.assert_series_equal(expected, result) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): result = ser.reindex_like(other, method="nearest", tolerance=1) tm.assert_series_equal(expected, result) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas40DeprecationWarning): result = ser.reindex_like(other, method="nearest", tolerance=[1, 2, 3, 4]) tm.assert_series_equal(expected, result)