diff --git a/doc/source/reference/testing.rst b/doc/source/reference/testing.rst index 1f164d1aa98b4..d91c7ba081850 100644 --- a/doc/source/reference/testing.rst +++ b/doc/source/reference/testing.rst @@ -51,6 +51,12 @@ Exceptions and warnings errors.OptionError errors.OutOfBoundsDatetime errors.OutOfBoundsTimedelta + errors.PandasChangeWarning + errors.Pandas4Warning + errors.Pandas5Warning + errors.PandasPendingDeprecationWarning + errors.PandasDeprecationWarning + errors.PandasFutureWarning errors.ParserError errors.ParserWarning errors.PerformanceWarning diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 099e5bc48353a..7a213aeaae25b 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -24,6 +24,12 @@ Enhancement1 Enhancement2 ^^^^^^^^^^^^ +New Deprecation Policy +^^^^^^^^^^^^^^^^^^^^^^ +pandas 3.0.0 introduces a new 3-stage deprecation policy: using ``DeprecationWarning`` initially, then switching to ``FutureWarning`` for broader visibility in the last minor version before the next major release, and then removal of the deprecated functionality in the major release. + +This was done to give downstream packages more time to adjust to pandas deprecations, which should reduce the amount of warnings that a user gets from code that isn't theirs. + .. _whatsnew_300.enhancements.other: Other enhancements diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 390267db8267f..5af4c80d0de8e 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1994,12 +1994,14 @@ class Timestamp(_Timestamp): >>> pd.Timestamp.utcnow() # doctest: +SKIP Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """ + from pandas.errors import Pandas4Warning + warnings.warn( # The stdlib datetime.utcnow is deprecated, so we deprecate to match. # GH#56680 "Timestamp.utcnow is deprecated and will be removed in a future " "version. Use Timestamp.now('UTC') instead.", - FutureWarning, + Pandas4Warning, stacklevel=find_stack_level(), ) return cls.now(UTC) @@ -2036,13 +2038,15 @@ class Timestamp(_Timestamp): >>> pd.Timestamp.utcfromtimestamp(1584199972) Timestamp('2020-03-14 15:32:52+0000', tz='UTC') """ + from pandas.errors import Pandas4Warning + # GH#22451 warnings.warn( # The stdlib datetime.utcfromtimestamp is deprecated, so we deprecate # to match. GH#56680 "Timestamp.utcfromtimestamp is deprecated and will be removed in a " "future version. Use Timestamp.fromtimestamp(ts, 'UTC') instead.", - FutureWarning, + Pandas4Warning, stacklevel=find_stack_level(), ) return cls.fromtimestamp(ts, tz="UTC") diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 44a215ab082b7..b7bd526c37493 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -55,6 +55,7 @@ from pandas.errors import ( ChainedAssignmentError, InvalidIndexError, + Pandas4Warning, ) from pandas.errors.cow import ( _chained_assignment_method_msg, @@ -11909,7 +11910,7 @@ def all( **kwargs, ) -> Series | bool: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="all") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="all") @doc(make_doc("all", ndim=1)) def all( self, @@ -11956,7 +11957,7 @@ def min( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="min") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="min") @doc(make_doc("min", ndim=2)) def min( self, @@ -12003,7 +12004,7 @@ def max( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="max") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="max") @doc(make_doc("max", ndim=2)) def max( self, @@ -12019,7 +12020,7 @@ def max( result = result.__finalize__(self, method="max") return result - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="sum") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sum") def sum( self, axis: Axis | None = 0, @@ -12120,7 +12121,7 @@ def sum( result = result.__finalize__(self, method="sum") return result - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="prod") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="prod") def prod( self, axis: Axis | None = 0, @@ -12238,7 +12239,7 @@ def mean( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="mean") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="mean") @doc(make_doc("mean", ndim=2)) def mean( self, @@ -12285,7 +12286,9 @@ def median( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="median") + @deprecate_nonkeyword_arguments( + Pandas4Warning, allowed_args=["self"], name="median" + ) @doc(make_doc("median", ndim=2)) def median( self, @@ -12335,7 +12338,7 @@ def sem( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="sem") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sem") def sem( self, axis: Axis | None = 0, @@ -12455,7 +12458,7 @@ def var( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="var") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="var") def var( self, axis: Axis | None = 0, @@ -12574,7 +12577,7 @@ def std( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="std") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="std") def std( self, axis: Axis | None = 0, @@ -12697,7 +12700,7 @@ def skew( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="skew") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="skew") def skew( self, axis: Axis | None = 0, @@ -12817,7 +12820,7 @@ def kurt( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="kurt") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="kurt") def kurt( self, axis: Axis | None = 0, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 13585d7de6beb..e72fd92236969 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -90,6 +90,7 @@ AbstractMethodError, ChainedAssignmentError, InvalidIndexError, + Pandas4Warning, ) from pandas.errors.cow import _chained_assignment_method_msg from pandas.util._decorators import ( @@ -2594,7 +2595,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, + Pandas4Warning, stacklevel=find_stack_level(), ) elif date_format == "epoch": @@ -2602,7 +2603,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, + Pandas4Warning, stacklevel=find_stack_level(), ) @@ -4381,12 +4382,12 @@ def _check_copy_deprecation(copy): "version. Copy-on-Write is active in pandas since 3.0 which utilizes " "a lazy copy mechanism that defers copies until necessary. Use " ".copy() to make an eager copy if necessary.", - DeprecationWarning, + Pandas4Warning, stacklevel=find_stack_level(), ) # issue 58667 - @deprecate_kwarg("method", None) + @deprecate_kwarg(Pandas4Warning, "method", new_arg_name=None) @final def reindex_like( self, diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index b520ad69aae96..0a7b23a4f0852 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -27,7 +27,10 @@ from pandas._libs import Interval from pandas._libs.hashtable import duplicated -from pandas.errors import SpecificationError +from pandas.errors import ( + Pandas4Warning, + SpecificationError, +) from pandas.util._decorators import ( Appender, Substitution, @@ -3330,7 +3333,7 @@ def corrwith( """ warnings.warn( "DataFrameGroupBy.corrwith is deprecated", - FutureWarning, + Pandas4Warning, stacklevel=find_stack_level(), ) result = self._op_via_apply( diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index c404323a1168c..b9a0af6b0bc28 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -14,6 +14,7 @@ import numpy as np from pandas._libs import lib +from pandas.errors import Pandas4Warning from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( @@ -218,7 +219,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, + Pandas4Warning, stacklevel=find_stack_level(), ) return cast(ArrowExtensionArray, self._parent.array)._dt_to_pytimedelta() @@ -501,7 +502,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, + Pandas4Warning, stacklevel=find_stack_level(), ) return self._get_values().to_pytimedelta() diff --git a/pandas/core/internals/api.py b/pandas/core/internals/api.py index 04944db2ebd9c..077b855771086 100644 --- a/pandas/core/internals/api.py +++ b/pandas/core/internals/api.py @@ -15,6 +15,7 @@ import numpy as np from pandas._libs.internals import BlockPlacement +from pandas.errors import Pandas4Warning from pandas.core.dtypes.common import pandas_dtype from pandas.core.dtypes.dtypes import ( @@ -93,7 +94,7 @@ def make_block( "make_block is deprecated and will be removed in a future version. " "Use pd.api.internals.create_dataframe_from_blocks or " "(recommended) higher-level public APIs instead.", - DeprecationWarning, + Pandas4Warning, stacklevel=2, ) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 5efaf0dc051bd..c74393363a068 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -17,6 +17,7 @@ import numpy as np from pandas._libs import lib +from pandas.errors import Pandas4Warning from pandas.util._decorators import set_module from pandas.util._exceptions import find_stack_level @@ -392,7 +393,7 @@ def concat( "version. Copy-on-Write is active in pandas since 3.0 which utilizes " "a lazy copy mechanism that defers copies until necessary. Use " ".copy() to make an eager copy if necessary.", - DeprecationWarning, + Pandas4Warning, stacklevel=find_stack_level(), ) if join == "outer": diff --git a/pandas/core/series.py b/pandas/core/series.py index 5ed094349caaa..f98e8c2563abb 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -40,6 +40,7 @@ from pandas.errors import ( ChainedAssignmentError, InvalidIndexError, + Pandas4Warning, ) from pandas.errors.cow import ( _chained_assignment_method_msg, @@ -1472,7 +1473,7 @@ def to_string( ) -> None: ... @deprecate_nonkeyword_arguments( - version="4.0", allowed_args=["self", "buf"], name="to_string" + Pandas4Warning, allowed_args=["self", "buf"], name="to_string" ) def to_string( self, @@ -1630,7 +1631,7 @@ def to_markdown( ), ) @deprecate_nonkeyword_arguments( - version="4.0", allowed_args=["self", "buf"], name="to_markdown" + Pandas4Warning, allowed_args=["self", "buf"], name="to_markdown" ) def to_markdown( self, @@ -6643,7 +6644,7 @@ def any( # type: ignore[override] filter_type="bool", ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="all") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="all") @Appender(make_doc("all", ndim=1)) def all( self, @@ -6663,7 +6664,7 @@ def all( filter_type="bool", ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="min") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="min") def min( self, axis: Axis | None = 0, @@ -6734,7 +6735,7 @@ def min( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="max") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="max") def max( self, axis: Axis | None = 0, @@ -6805,7 +6806,7 @@ def max( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="sum") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sum") def sum( self, axis: Axis | None = None, @@ -6906,7 +6907,7 @@ def sum( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="prod") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="prod") @doc(make_doc("prod", ndim=1)) def prod( self, @@ -6925,7 +6926,7 @@ def prod( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="mean") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="mean") def mean( self, axis: Axis | None = 0, @@ -6979,7 +6980,9 @@ def mean( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="median") + @deprecate_nonkeyword_arguments( + Pandas4Warning, allowed_args=["self"], name="median" + ) def median( self, axis: Axis | None = 0, @@ -7060,7 +7063,7 @@ def median( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="sem") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sem") @doc(make_doc("sem", ndim=1)) def sem( self, @@ -7079,7 +7082,7 @@ def sem( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="var") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="var") def var( self, axis: Axis | None = None, @@ -7166,7 +7169,7 @@ def var( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="std") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="std") @doc(make_doc("std", ndim=1)) def std( self, @@ -7185,7 +7188,7 @@ def std( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="skew") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="skew") @doc(make_doc("skew", ndim=1)) def skew( self, @@ -7198,7 +7201,7 @@ def skew( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="kurt") + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="kurt") def kurt( self, axis: Axis | None = 0, diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 2b5bc450e41d6..57be6a07c464d 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -91,6 +91,96 @@ class PerformanceWarning(Warning): """ +class PandasChangeWarning(Warning): + """ + Warning raised for any an upcoming change. + + See Also + -------- + errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + + Examples + -------- + >>> pd.errors.PandasChangeWarning + + """ + + +class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning): + """ + Warning raised for an upcoming change that is a PendingDeprecationWarning. + + See Also + -------- + errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + + Examples + -------- + >>> pd.errors.PandasPendingDeprecationWarning + + """ + + +class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning): + """ + Warning raised for an upcoming change that is a DeprecationWarning. + + See Also + -------- + errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + + Examples + -------- + >>> pd.errors.PandasDeprecationWarning + + """ + + +class PandasFutureWarning(PandasChangeWarning, FutureWarning): + """ + Warning raised for an upcoming change that is a FutureWarning. + + See Also + -------- + errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + + Examples + -------- + >>> pd.errors.PandasFutureWarning + + """ + + +class Pandas4Warning(PandasDeprecationWarning): + """ + Warning raised for an upcoming change that will be enforced in pandas 4.0. + + See Also + -------- + errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + + Examples + -------- + >>> pd.errors.Pandas4Warning + + """ + + +class Pandas5Warning(PandasPendingDeprecationWarning): + """ + Warning raised for an upcoming change that will be enforced in pandas 5.0. + + See Also + -------- + errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + + Examples + -------- + >>> pd.errors.Pandas5Warning + + """ + + class UnsupportedFunctionCall(ValueError): """ Exception raised when attempting to call a unsupported numpy function. @@ -932,6 +1022,12 @@ class InvalidComparison(Exception): "OptionError", "OutOfBoundsDatetime", "OutOfBoundsTimedelta", + "Pandas4Warning", + "Pandas5Warning", + "PandasChangeWarning", + "PandasDeprecationWarning", + "PandasFutureWarning", + "PandasPendingDeprecationWarning", "ParserError", "ParserWarning", "PerformanceWarning", diff --git a/pandas/io/feather_format.py b/pandas/io/feather_format.py index 565c53f0f3fc5..ebb678c26db30 100644 --- a/pandas/io/feather_format.py +++ b/pandas/io/feather_format.py @@ -12,6 +12,7 @@ from pandas._libs import lib from pandas.compat._optional import import_optional_dependency +from pandas.errors import Pandas4Warning from pandas.util._decorators import doc from pandas.util._validators import check_dtype_backend @@ -144,7 +145,7 @@ def read_feather( warnings.filterwarnings( "ignore", "make_block is deprecated", - DeprecationWarning, + Pandas4Warning, ) return feather.read_feather( diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py index 6a5a83088e986..dcbb41f9ad233 100644 --- a/pandas/io/parquet.py +++ b/pandas/io/parquet.py @@ -17,7 +17,10 @@ from pandas._libs import lib from pandas.compat._optional import import_optional_dependency -from pandas.errors import AbstractMethodError +from pandas.errors import ( + AbstractMethodError, + Pandas4Warning, +) from pandas.util._decorators import doc from pandas.util._validators import check_dtype_backend @@ -265,7 +268,7 @@ def read( filterwarnings( "ignore", "make_block is deprecated", - DeprecationWarning, + Pandas4Warning, ) result = arrow_table_to_pandas( pa_table, @@ -393,7 +396,7 @@ def read( filterwarnings( "ignore", "make_block is deprecated", - DeprecationWarning, + Pandas4Warning, ) return parquet_file.to_pandas( columns=columns, filters=filters, **kwargs diff --git a/pandas/io/parsers/arrow_parser_wrapper.py b/pandas/io/parsers/arrow_parser_wrapper.py index 8cadde1ad6537..5edf0432c55fe 100644 --- a/pandas/io/parsers/arrow_parser_wrapper.py +++ b/pandas/io/parsers/arrow_parser_wrapper.py @@ -6,10 +6,13 @@ from pandas._libs import lib from pandas.compat._optional import import_optional_dependency from pandas.errors import ( + Pandas4Warning, ParserError, ParserWarning, ) -from pandas.util._exceptions import find_stack_level +from pandas.util._exceptions import ( + find_stack_level, +) from pandas.core.dtypes.common import pandas_dtype from pandas.core.dtypes.inference import is_integer @@ -286,7 +289,7 @@ def read(self) -> DataFrame: warnings.filterwarnings( "ignore", "make_block is deprecated", - DeprecationWarning, + Pandas4Warning, ) frame = arrow_table_to_pandas( table, dtype_backend=dtype_backend, null_to_int64=True diff --git a/pandas/tests/copy_view/test_copy_deprecation.py b/pandas/tests/copy_view/test_copy_deprecation.py index 8ee37213b92ab..67ee804a755fb 100644 --- a/pandas/tests/copy_view/test_copy_deprecation.py +++ b/pandas/tests/copy_view/test_copy_deprecation.py @@ -1,5 +1,7 @@ import pytest +from pandas.errors import Pandas4Warning + import pandas as pd from pandas import ( concat, @@ -38,11 +40,11 @@ def test_copy_deprecation(meth, kwargs): df = df.set_index(["b", "c"]) if meth != "swaplevel": - with tm.assert_produces_warning(DeprecationWarning, match="copy"): + with tm.assert_produces_warning(Pandas4Warning, match="copy"): getattr(df, meth)(copy=False, **kwargs) if meth != "transpose": - with tm.assert_produces_warning(DeprecationWarning, match="copy"): + with tm.assert_produces_warning(Pandas4Warning, match="copy"): getattr(df.a, meth)(copy=False, **kwargs) @@ -50,22 +52,22 @@ def test_copy_deprecation_reindex_like_align(): df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) # Somehow the stack level check is incorrect here with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): df.reindex_like(df, copy=False) with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): df.a.reindex_like(df.a, copy=False) with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): df.align(df, copy=False) with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): df.a.align(df.a, copy=False) @@ -74,16 +76,16 @@ def test_copy_deprecation_merge_concat(): df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): df.merge(df, copy=False) with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): merge(df, df, copy=False) with tm.assert_produces_warning( - DeprecationWarning, match="copy", check_stacklevel=False + Pandas4Warning, match="copy", check_stacklevel=False ): concat([df, df], copy=False) diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index fc5930ebcd8ac..2aa7aec23c707 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -45,6 +45,7 @@ pa_version_under19p0, pa_version_under20p0, ) +from pandas.errors import Pandas4Warning from pandas.core.dtypes.dtypes import ( ArrowDtype, @@ -2849,14 +2850,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(Pandas4Warning, 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(Pandas4Warning, 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..73e3d2ecd6215 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.errors import Pandas4Warning + 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(Pandas4Warning): 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(Pandas4Warning): result = df.reindex_like(df, method=method, tolerance=[0, 0, 0, 0]) tm.assert_frame_equal(df, result) diff --git a/pandas/tests/groupby/test_all_methods.py b/pandas/tests/groupby/test_all_methods.py index 4625c5c27a803..2310c3bf59e15 100644 --- a/pandas/tests/groupby/test_all_methods.py +++ b/pandas/tests/groupby/test_all_methods.py @@ -13,6 +13,8 @@ import pytest +from pandas.errors import Pandas4Warning + import pandas as pd from pandas import DataFrame import pandas._testing as tm @@ -26,7 +28,7 @@ def test_multiindex_group_all_columns_when_empty(groupby_func): method = getattr(gb, groupby_func) args = get_groupby_method_args(groupby_func, df) if groupby_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None @@ -72,7 +74,7 @@ def test_dup_labels_output_shape(groupby_func, idx): args = get_groupby_method_args(groupby_func, df) if groupby_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 5bf16ee9ad0b8..635393e41bd9d 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -6,6 +6,8 @@ import numpy as np import pytest +from pandas.errors import Pandas4Warning + import pandas as pd from pandas import ( DataFrame, @@ -1109,7 +1111,7 @@ def test_apply_is_unchanged_when_other_methods_are_called_first(reduction_func): grp = df.groupby(by="a") args = get_groupby_method_args(reduction_func, df) if reduction_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index cae3013642739..97c2be7584837 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -3,6 +3,8 @@ import numpy as np import pytest +from pandas.errors import Pandas4Warning + import pandas as pd from pandas import ( Categorical, @@ -1488,7 +1490,7 @@ def test_dataframe_groupby_on_2_categoricals_when_observed_is_true(reduction_fun args = get_groupby_method_args(reduction_func, df) if reduction_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None @@ -1534,7 +1536,7 @@ def test_dataframe_groupby_on_2_categoricals_when_observed_is_false( return if reduction_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None @@ -1933,7 +1935,7 @@ def test_category_order_reducer( getattr(gb, reduction_func)(*args) return if reduction_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None diff --git a/pandas/tests/groupby/test_groupby_dropna.py b/pandas/tests/groupby/test_groupby_dropna.py index 8c4ab42b7be7a..c6368385602f5 100644 --- a/pandas/tests/groupby/test_groupby_dropna.py +++ b/pandas/tests/groupby/test_groupby_dropna.py @@ -2,6 +2,7 @@ import pytest from pandas.compat.pyarrow import pa_version_under10p1 +from pandas.errors import Pandas4Warning from pandas.core.dtypes.missing import na_value_for_dtype @@ -542,7 +543,7 @@ def test_categorical_reducers(reduction_func, observed, sort, as_index, index_ki gb_filled = df_filled.groupby(keys, observed=observed, sort=sort, as_index=True) if reduction_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None @@ -573,7 +574,7 @@ def test_categorical_reducers(reduction_func, observed, sort, as_index, index_ki expected = expected["size"].rename(None) if reduction_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None diff --git a/pandas/tests/groupby/test_numeric_only.py b/pandas/tests/groupby/test_numeric_only.py index 99a88a5d8fe7c..b79ca8bf1ee3a 100644 --- a/pandas/tests/groupby/test_numeric_only.py +++ b/pandas/tests/groupby/test_numeric_only.py @@ -3,6 +3,7 @@ import pytest from pandas._libs import lib +from pandas.errors import Pandas4Warning import pandas as pd from pandas import ( @@ -266,7 +267,7 @@ def test_numeric_only(kernel, has_arg, numeric_only, keys): if has_arg and numeric_only is True: # Cases where b does not appear in the result if kernel == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None @@ -311,7 +312,7 @@ def test_numeric_only(kernel, has_arg, numeric_only, keys): msg = "'>' not supported between instances of 'type' and 'type'" with pytest.raises(exception, match=msg): if kernel == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None diff --git a/pandas/tests/groupby/test_raises.py b/pandas/tests/groupby/test_raises.py index 864b9e5d55991..1473b95bf9476 100644 --- a/pandas/tests/groupby/test_raises.py +++ b/pandas/tests/groupby/test_raises.py @@ -8,6 +8,8 @@ import numpy as np import pytest +from pandas.errors import Pandas4Warning + from pandas import ( Categorical, DataFrame, @@ -84,9 +86,12 @@ def df_with_cat_col(): return df -def _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=""): - warn_klass = None if warn_msg == "" else FutureWarning - with tm.assert_produces_warning(warn_klass, match=warn_msg, check_stacklevel=False): +def _call_and_check( + klass, msg, how, gb, groupby_func, args, warn_category=None, warn_msg="" +): + with tm.assert_produces_warning( + warn_category, match=warn_msg, check_stacklevel=False + ): if klass is None: if how == "method": getattr(gb, groupby_func)(*args) @@ -213,10 +218,12 @@ def test_groupby_raises_string( msg = "Cannot perform reduction 'mean' with string dtype" if groupby_func == "corrwith": + warn_category = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: + warn_category = None warn_msg = "" - _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg) + _call_and_check(klass, msg, how, gb, groupby_func, args, warn_category, warn_msg) @pytest.mark.parametrize("how", ["agg", "transform"]) @@ -337,10 +344,12 @@ def test_groupby_raises_datetime( }[groupby_func] if groupby_func == "corrwith": + warn_category = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: + warn_category = None warn_msg = "" - _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=warn_msg) + _call_and_check(klass, msg, how, gb, groupby_func, args, warn_category, warn_msg) @pytest.mark.parametrize("how", ["agg", "transform"]) @@ -541,10 +550,12 @@ def test_groupby_raises_category( }[groupby_func] if groupby_func == "corrwith": + warn_category = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: + warn_category = None warn_msg = "" - _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg) + _call_and_check(klass, msg, how, gb, groupby_func, args, warn_category, warn_msg) @pytest.mark.parametrize("how", ["agg", "transform"]) @@ -724,7 +735,9 @@ def test_groupby_raises_category_on_category( }[groupby_func] if groupby_func == "corrwith": + warn_category = Pandas4Warning warn_msg = "DataFrameGroupBy.corrwith is deprecated" else: + warn_category = None warn_msg = "" - _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg) + _call_and_check(klass, msg, how, gb, groupby_func, args, warn_category, warn_msg) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index fecd20fd6cece..59092385fbf3b 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -4,6 +4,7 @@ import pytest from pandas._libs import lib +from pandas.errors import Pandas4Warning from pandas.core.dtypes.common import ensure_platform_int @@ -1103,7 +1104,7 @@ def test_transform_agg_by_name(request, reduction_func, frame_or_series): args = get_groupby_method_args(reduction_func, obj) if func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None @@ -1470,7 +1471,7 @@ def test_as_index_no_change(keys, df, groupby_func): gb_as_index_true = df.groupby(keys, as_index=True) gb_as_index_false = df.groupby(keys, as_index=False) if groupby_func == "corrwith": - warn = FutureWarning + warn = Pandas4Warning msg = "DataFrameGroupBy.corrwith is deprecated" else: warn = None diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index f3d9b88cc91e2..9b1e22ce1bb06 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -2,6 +2,8 @@ import pytest +from pandas.errors import Pandas4Warning + import pandas as pd import pandas._testing as tm @@ -15,7 +17,7 @@ def test_keyword_deprecation(): "except for the argument 'buf' will be keyword-only." ) s = pd.Series() - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): s.to_markdown(None, "wt") diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index 0866581535c2f..1d414cef152c7 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -12,6 +12,8 @@ from pandas._config import using_string_dtype +from pandas.errors import Pandas4Warning + from pandas import ( CategoricalIndex, DataFrame, @@ -42,7 +44,7 @@ def test_keyword_deprecation(self): "except for the argument 'buf' will be keyword-only." ) s = Series(["a", "b"]) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): s.to_string(None, "NaN") def test_to_string_masked_ea_with_formatter(self): diff --git a/pandas/tests/io/json/test_json_table_schema.py b/pandas/tests/io/json/test_json_table_schema.py index 7936982e4a055..769249ef4c47b 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.errors import Pandas4Warning + from pandas.core.dtypes.dtypes import ( CategoricalDtype, DatetimeTZDtype, @@ -460,7 +462,7 @@ 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(Pandas4Warning, 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 32eeb30de4b69..62f0cc50aea64 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -15,6 +15,7 @@ from pandas._config import using_string_dtype from pandas.compat import IS64 +from pandas.errors import Pandas4Warning import pandas.util._test_decorators as td import pandas as pd @@ -145,7 +146,7 @@ def test_frame_non_unique_columns(self, orient, data, request): "in a future version, please use 'iso' date format instead." ) if df.iloc[:, 0].dtype == "datetime64[s]": - expected_warning = FutureWarning + expected_warning = Pandas4Warning with tm.assert_produces_warning(expected_warning, match=msg): result = read_json( @@ -780,7 +781,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(Pandas4Warning, match=msg): data = StringIO(s.to_json()) result = read_json(data, typ="series", dtype=dtype) tm.assert_series_equal(result, expected) @@ -831,13 +832,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(Pandas4Warning, 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(Pandas4Warning, match=msg): json = StringIO(df.to_json(date_unit="ns")) result = read_json(json, convert_dates=False) @@ -848,7 +849,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(Pandas4Warning, match=msg): json = StringIO(ts.to_json()) result = read_json(json, typ="series") tm.assert_series_equal(result, ts) @@ -870,7 +871,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 = Pandas4Warning else: expected = ( '{"2020-01-01T00:00:00.000":"2020-01-01T00:00:00.000","null":null}' @@ -984,7 +985,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(Pandas4Warning, match=msg): json = df.to_json(date_format="epoch", date_unit=unit) # force date unit @@ -1004,13 +1005,13 @@ def test_date_unit(self, unit, datetime_frame): DataFrame( {"A": ["a", "b", "c"], "B": pd.to_timedelta(np.arange(3), unit="D")} ), - FutureWarning, + Pandas4Warning, ), ( DataFrame( {"A": pd.to_datetime(["2020-01-01", "2020-02-01", "2020-03-01"])} ), - FutureWarning, + Pandas4Warning, ), ], ) @@ -1096,7 +1097,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(Pandas4Warning, match=msg): json = StringIO(dfj2.to_json()) result = read_json(json, dtype={"ints": np.int64, "bools": np.bool_}) tm.assert_frame_equal(result, result) @@ -1138,20 +1139,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(Pandas4Warning, 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(Pandas4Warning, 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(Pandas4Warning, match=msg): json = frame.to_json() tm.assert_frame_equal(frame, read_json(StringIO(json)).apply(converter)) @@ -1167,7 +1168,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(Pandas4Warning, match=msg): data = StringIO(frame.to_json(date_unit="ns")) result = read_json(data) result["a"] = pd.to_timedelta(result.a, unit="ns") @@ -1202,7 +1203,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 = Pandas4Warning expected = '{"86400000":86400000,"172800000":172800000,"null":null}' if as_object: @@ -1221,7 +1222,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 = Pandas4Warning if as_object: ser = ser.astype(object) warn = None @@ -1315,13 +1316,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(Pandas4Warning, 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(Pandas4Warning, match=msg): assert stz.to_json() == s_naive.to_json() def test_sparse(self): @@ -1588,7 +1589,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(Pandas4Warning, match=msg): dfjson = expected.to_json(orient=orient) result = read_json( @@ -1790,7 +1791,7 @@ def test_timedelta_as_label(self, date_format, key): expected_warning = None if date_format == "epoch": - expected_warning = FutureWarning + expected_warning = Pandas4Warning msg = ( "'epoch' date format is deprecated and will be removed in a future " @@ -2032,7 +2033,7 @@ def test_json_pandas_nulls(self, nulls_fixture): "in a future version, please use 'iso' date format instead." ) if nulls_fixture is pd.NaT: - expected_warning = FutureWarning + expected_warning = Pandas4Warning 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 f68ef5fa2e0e5..f5a2a811ff433 100644 --- a/pandas/tests/io/test_gcs.py +++ b/pandas/tests/io/test_gcs.py @@ -8,6 +8,7 @@ import pytest from pandas.compat.pyarrow import pa_version_under17p0 +from pandas.errors import Pandas4Warning from pandas import ( DataFrame, @@ -84,7 +85,7 @@ def test_to_read_gcs(gcs_buffer, format, monkeypatch, capsys, request): "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(Pandas4Warning, 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 f871c0bf0218c..a3f97c818e335 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.errors import Pandas4Warning import pandas.util._test_decorators as td import pandas as pd @@ -1313,7 +1314,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(Pandas4Warning): 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/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 2c97c4a32e0aa..be624855ca2a5 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -17,7 +17,10 @@ import pytest from pandas._libs.tslibs.dtypes import NpyDatetimeUnit -from pandas.errors import OutOfBoundsDatetime +from pandas.errors import ( + OutOfBoundsDatetime, + Pandas4Warning, +) from pandas import ( NA, @@ -325,13 +328,13 @@ class TestTimestampClassMethodConstructors: def test_utcnow_deprecated(self): # GH#56680 msg = "Timestamp.utcnow is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): Timestamp.utcnow() def test_utcfromtimestamp_deprecated(self): # GH#56680 msg = "Timestamp.utcfromtimestamp is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): Timestamp.utcfromtimestamp(43) def test_constructor_strptime(self): diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 38d0ddfbc13bd..345386025b209 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -30,6 +30,7 @@ tz_compare, ) from pandas.compat import IS64 +from pandas.errors import Pandas4Warning from pandas import ( NaT, @@ -268,7 +269,7 @@ def test_disallow_setting_tz(self, tz): def test_default_to_stdlib_utc(self): msg = "Timestamp.utcnow is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): assert Timestamp.utcnow().tz is timezone.utc assert Timestamp.now("UTC").tz is timezone.utc assert Timestamp("2016-01-01", tz="UTC").tz is timezone.utc @@ -313,13 +314,13 @@ def compare(x, y): compare(Timestamp.now("UTC"), datetime.now(timezone.utc)) compare(Timestamp.now("UTC"), datetime.now(tzutc())) msg = "Timestamp.utcnow is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): compare(Timestamp.utcnow(), datetime.now(timezone.utc)) compare(Timestamp.today(), datetime.today()) current_time = calendar.timegm(datetime.now().utctimetuple()) msg = "Timestamp.utcfromtimestamp is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(Pandas4Warning, match=msg): ts_utc = Timestamp.utcfromtimestamp(current_time) assert ts_utc.timestamp() == current_time compare( diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index f017ccd963972..7fb33f7d09d97 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.errors import Pandas4Warning + 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(Pandas4Warning) 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 2c441a6ed91c1..f69c90ced2828 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -11,6 +11,7 @@ import pytest from pandas._libs.tslibs.timezones import maybe_get_tz +from pandas.errors import Pandas4Warning 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(Pandas4Warning, 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..15cd1502dadfb 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.errors import Pandas4Warning + 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(Pandas4Warning): 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(Pandas4Warning): result = ser.reindex_like(other, method="nearest") tm.assert_series_equal(expected, result) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(Pandas4Warning): 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(Pandas4Warning): result = ser.reindex_like(other, method="nearest", tolerance=[1, 2, 3, 4]) tm.assert_series_equal(expected, result) diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index c5c4b234eb129..02b784a187f88 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -1,11 +1,19 @@ +import warnings + import pytest from pandas.errors import ( AbstractMethodError, + Pandas4Warning, + Pandas5Warning, + PandasChangeWarning, + PandasDeprecationWarning, + PandasPendingDeprecationWarning, UndefinedVariableError, ) import pandas as pd +import pandas._testing as tm @pytest.mark.parametrize( @@ -102,3 +110,20 @@ def test_AbstractMethodError_classmethod(): xpr = "This method must be defined in the concrete class Foo" with pytest.raises(AbstractMethodError, match=xpr): Foo().method() + + +@pytest.mark.parametrize( + "warn_category, filter_category", + [ + (Pandas4Warning, PandasChangeWarning), + (Pandas4Warning, PandasDeprecationWarning), + (Pandas5Warning, PandasChangeWarning), + (Pandas5Warning, PandasPendingDeprecationWarning), + ], +) +def test_pandas_warnings_filter(warn_category, filter_category): + # https://github.com/pandas-dev/pandas/pull/61468 + # Ensure users can suppress warnings. + with tm.assert_produces_warning(None), warnings.catch_warnings(): + warnings.filterwarnings(category=filter_category, action="ignore") + warnings.warn("test", category=warn_category) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index b02fab70fb825..74f02a0b33396 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -1051,7 +1051,9 @@ def test_to_datetime_today(self, tz): def test_to_datetime_today_now_unicode_bytes(self, arg): to_datetime([arg]) - @pytest.mark.filterwarnings("ignore:Timestamp.utcnow is deprecated:FutureWarning") + @pytest.mark.filterwarnings( + "ignore:Timestamp.utcnow is deprecated:DeprecationWarning" + ) @pytest.mark.skipif(WASM, reason="tzset is not available on WASM") @pytest.mark.parametrize( "format, expected_ds", diff --git a/pandas/tests/util/test_deprecate.py b/pandas/tests/util/test_deprecate.py index 92f422b8269f5..94c8fe7fd45d1 100644 --- a/pandas/tests/util/test_deprecate.py +++ b/pandas/tests/util/test_deprecate.py @@ -37,7 +37,9 @@ def new_func_with_deprecation(): def test_deprecate_ok(): - depr_func = deprecate("depr_func", new_func, "1.0", msg="Use new_func instead.") + depr_func = deprecate( + FutureWarning, "depr_func", new_func, "1.0", msg="Use new_func instead." + ) with tm.assert_produces_warning(FutureWarning): result = depr_func() @@ -48,7 +50,11 @@ def test_deprecate_ok(): def test_deprecate_no_docstring(): depr_func = deprecate( - "depr_func", new_func_no_docstring, "1.0", msg="Use new_func instead." + FutureWarning, + "depr_func", + new_func_no_docstring, + "1.0", + msg="Use new_func instead.", ) with tm.assert_produces_warning(FutureWarning): result = depr_func() @@ -59,5 +65,9 @@ def test_deprecate_wrong_docstring(): msg = "deprecate needs a correctly formatted docstring" with pytest.raises(AssertionError, match=msg): deprecate( - "depr_func", new_func_wrong_docstring, "1.0", msg="Use new_func instead." + FutureWarning, + "depr_func", + new_func_wrong_docstring, + "1.0", + msg="Use new_func instead.", ) diff --git a/pandas/tests/util/test_deprecate_kwarg.py b/pandas/tests/util/test_deprecate_kwarg.py index b165e9fba0e4f..ae3519638ca03 100644 --- a/pandas/tests/util/test_deprecate_kwarg.py +++ b/pandas/tests/util/test_deprecate_kwarg.py @@ -5,7 +5,7 @@ import pandas._testing as tm -@deprecate_kwarg("old", "new") +@deprecate_kwarg(FutureWarning, "old", new_arg_name="new") def _f1(new=False): return new @@ -13,7 +13,7 @@ def _f1(new=False): _f2_mappings = {"yes": True, "no": False} -@deprecate_kwarg("old", "new", _f2_mappings) +@deprecate_kwarg(FutureWarning, "old", new_arg_name="new", mapping=_f2_mappings) def _f2(new=False): return new @@ -22,7 +22,7 @@ def _f3_mapping(x): return x + 1 -@deprecate_kwarg("old", "new", _f3_mapping) +@deprecate_kwarg(FutureWarning, "old", new_arg_name="new", mapping=_f3_mapping) def _f3(new=0): return new @@ -65,12 +65,12 @@ def test_bad_deprecate_kwarg(): with pytest.raises(TypeError, match=msg): - @deprecate_kwarg("old", "new", 0) + @deprecate_kwarg(FutureWarning, "old", "new", 0) def f4(new=None): return new -@deprecate_kwarg("old", None) +@deprecate_kwarg(FutureWarning, "old", new_arg_name=None) def _f4(old=True, unchanged=True): return old, unchanged diff --git a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py index f81d32b574682..8ca6482096da7 100644 --- a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py +++ b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py @@ -4,13 +4,16 @@ import inspect +from pandas.errors import Pandas4Warning from pandas.util._decorators import deprecate_nonkeyword_arguments import pandas._testing as tm +WARNING_CATEGORY = Pandas4Warning + @deprecate_nonkeyword_arguments( - version="1.1", allowed_args=["a", "b"], name="f_add_inputs" + WARNING_CATEGORY, allowed_args=["a", "b"], name="f_add_inputs" ) def f(a, b=0, c=0, d=0): return a + b + c + d @@ -41,25 +44,25 @@ def test_two_and_two_arguments(): def test_three_arguments(): - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(WARNING_CATEGORY): assert f(6, 3, 3) == 12 def test_four_arguments(): - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(WARNING_CATEGORY): assert f(1, 2, 3, 4) == 10 def test_three_arguments_with_name_in_warning(): msg = ( - "Starting with pandas version 1.1 all arguments of f_add_inputs " + "Starting with pandas version 4.0 all arguments of f_add_inputs " "except for the arguments 'a' and 'b' will be keyword-only." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): assert f(6, 3, 3) == 12 -@deprecate_nonkeyword_arguments(version="1.1") +@deprecate_nonkeyword_arguments(WARNING_CATEGORY) def g(a, b=0, c=0, d=0): with tm.assert_produces_warning(None): return a + b + c + d @@ -75,20 +78,20 @@ def test_one_and_three_arguments_default_allowed_args(): def test_three_arguments_default_allowed_args(): - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(WARNING_CATEGORY): assert g(6, 3, 3) == 12 def test_three_positional_argument_with_warning_message_analysis(): msg = ( - "Starting with pandas version 1.1 all arguments of g " + "Starting with pandas version 4.0 all arguments of g " "except for the argument 'a' will be keyword-only." ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): assert g(6, 3, 3) == 12 -@deprecate_nonkeyword_arguments(version="1.1") +@deprecate_nonkeyword_arguments(WARNING_CATEGORY) def h(a=0, b=0, c=0, d=0): return a + b + c + d @@ -103,17 +106,17 @@ def test_all_keyword_arguments(): def test_one_positional_argument(): - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(WARNING_CATEGORY): assert h(23) == 23 def test_one_positional_argument_with_warning_message_analysis(): - msg = "Starting with pandas version 1.1 all arguments of h will be keyword-only." - with tm.assert_produces_warning(FutureWarning, match=msg): + msg = "Starting with pandas version 4.0 all arguments of h will be keyword-only." + with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): assert h(19) == 19 -@deprecate_nonkeyword_arguments(version="1.1") +@deprecate_nonkeyword_arguments(WARNING_CATEGORY) def i(a=0, /, b=0, *, c=0, d=0): return a + b + c + d @@ -122,8 +125,13 @@ def test_i_signature(): assert str(inspect.signature(i)) == "(*, a=0, b=0, c=0, d=0)" +def test_i_warns_klass(): + with tm.assert_produces_warning(WARNING_CATEGORY): + assert i(1, 2) == 3 + + class Foo: - @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "bar"]) + @deprecate_nonkeyword_arguments(WARNING_CATEGORY, allowed_args=["self", "bar"]) def baz(self, bar=None, foobar=None): ... @@ -133,8 +141,8 @@ def test_foo_signature(): def test_class(): msg = ( - r"In a future version of pandas all arguments of Foo\.baz " + r"Starting with pandas version 4.0 all arguments of Foo\.baz " r"except for the argument \'bar\' will be keyword-only" ) - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): Foo().baz("qux", "quox") diff --git a/pandas/tests/util/test_pandas_deprecation_warning.py b/pandas/tests/util/test_pandas_deprecation_warning.py new file mode 100644 index 0000000000000..fe9d04f3ab10d --- /dev/null +++ b/pandas/tests/util/test_pandas_deprecation_warning.py @@ -0,0 +1,25 @@ +import warnings + +from pandas.errors import PandasChangeWarning +from pandas.util._decorators import deprecate_kwarg + +import pandas._testing as tm + + +def f1(): + warnings.warn("f1", PandasChangeWarning) + + +def test_function_warns_pandas_deprecation_warning(): + with tm.assert_produces_warning(PandasChangeWarning): + f1() + + +@deprecate_kwarg(PandasChangeWarning, "old", new_arg_name="new") +def f2(new=0): + return new + + +def test_decorator_warns_pandas_deprecation_warning(): + with tm.assert_produces_warning(PandasChangeWarning): + f2(old=1) diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index a1a0d51a7c72b..803b608d282bf 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -25,11 +25,11 @@ def deprecate( + klass: type[Warning], name: str, alternative: Callable[..., Any], version: str, alt_name: str | None = None, - klass: type[Warning] | None = None, stacklevel: int = 2, msg: str | None = None, ) -> Callable[[F], F]: @@ -44,6 +44,8 @@ def deprecate( Parameters ---------- + klass : Warning + The warning class to use. name : str Name of function to deprecate. alternative : func @@ -52,14 +54,12 @@ def deprecate( Version of pandas in which the method has been deprecated. alt_name : str, optional Name to use in preference of alternative.__name__. - klass : Warning, default FutureWarning stacklevel : int, default 2 msg : str The message to display in the warning. Default is '{name} is deprecated. Use {alt_name} instead.' """ alt_name = alt_name or alternative.__name__ - klass = klass or FutureWarning warning_msg = msg or f"{name} is deprecated, use {alt_name} instead." @wraps(alternative) @@ -100,6 +100,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: def deprecate_kwarg( + klass: type[Warning], old_arg_name: str, new_arg_name: str | None, mapping: Mapping[Any, Any] | Callable[[Any], Any] | None = None, @@ -110,8 +111,10 @@ def deprecate_kwarg( Parameters ---------- + klass : Warning + The warning class to use. old_arg_name : str - Name of argument in function to deprecate + Name of argument in function to deprecate. new_arg_name : str or None Name of preferred argument in function. Use None to raise warning that ``old_arg_name`` keyword is deprecated. @@ -119,12 +122,13 @@ def deprecate_kwarg( If mapping is present, use it to translate old arguments to new arguments. A callable must do its own value checking; values not found in a dict will be forwarded unchanged. + stacklevel : int, default 2 Examples -------- The following deprecates 'cols', using 'columns' instead - >>> @deprecate_kwarg(old_arg_name="cols", new_arg_name="columns") + >>> @deprecate_kwarg(FutureWarning, old_arg_name="cols", new_arg_name="columns") ... def f(columns=""): ... print(columns) >>> f(columns="should work ok") @@ -138,7 +142,7 @@ def deprecate_kwarg( >>> f(cols="should error", columns="can't pass do both") # doctest: +SKIP TypeError: Can only specify 'cols' or 'columns', not both - >>> @deprecate_kwarg("old", "new", {"yes": True, "no": False}) + >>> @deprecate_kwarg(FutureWarning, "old", "new", {"yes": True, "no": False}) ... def f(new=False): ... print("yes!" if new else "no!") >>> f(old="yes") # doctest: +SKIP @@ -148,19 +152,19 @@ def deprecate_kwarg( To raise a warning that a keyword will be removed entirely in the future - >>> @deprecate_kwarg(old_arg_name="cols", new_arg_name=None) + >>> @deprecate_kwarg(FutureWarning, old_arg_name="cols", new_arg_name=None) ... def f(cols="", another_param=""): ... print(cols) >>> f(cols="should raise warning") # doctest: +SKIP FutureWarning: the 'cols' keyword is deprecated and will be removed in a - future version please takes steps to stop use of 'cols' + future version. Please take steps to stop the use of 'cols' should raise warning >>> f(another_param="should not raise warning") # doctest: +SKIP should not raise warning >>> f(cols="should raise warning", another_param="") # doctest: +SKIP FutureWarning: the 'cols' keyword is deprecated and will be removed in a - future version please takes steps to stop use of 'cols' + future version. Please take steps to stop the use of 'cols' should raise warning """ if mapping is not None and not hasattr(mapping, "get") and not callable(mapping): @@ -180,7 +184,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: "will be removed in a future version. Please take " f"steps to stop the use of {old_arg_name!r}" ) - warnings.warn(msg, FutureWarning, stacklevel=stacklevel) + warnings.warn(msg, klass, stacklevel=stacklevel) kwargs[old_arg_name] = old_arg_value return func(*args, **kwargs) @@ -201,7 +205,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: f"use {new_arg_name!r} instead." ) - warnings.warn(msg, FutureWarning, stacklevel=stacklevel) + warnings.warn(msg, klass, stacklevel=stacklevel) if kwargs.get(new_arg_name) is not None: msg = ( f"Can only specify {old_arg_name!r} " @@ -263,7 +267,7 @@ def future_version_msg(version: str | None) -> str: def deprecate_nonkeyword_arguments( - version: str | None, + klass: type[Warning], allowed_args: list[str] | None = None, name: str | None = None, ) -> Callable[[F], F]: @@ -272,23 +276,30 @@ def deprecate_nonkeyword_arguments( Parameters ---------- - version : str, optional - The version in which positional arguments will become - keyword-only. If None, then the warning message won't - specify any particular version. - + klass : Warning, optional + The warning class to use. allowed_args : list, optional In case of list, it must be the list of names of some first arguments of the decorated functions that are OK to be given as positional arguments. In case of None value, defaults to list of all arguments not having the default value. - name : str, optional The specific name of the function to show in the warning message. If None, then the Qualified name of the function is used. """ + from pandas.errors import ( + Pandas4Warning, + Pandas5Warning, + ) + + if klass is Pandas4Warning: + version = "4.0" + elif klass is Pandas5Warning: + version = "5.0" + else: + raise AssertionError(f"{type(klass)=} must be a versioned warning") def decorate(func): old_sig = inspect.signature(func) @@ -326,7 +337,7 @@ def wrapper(*args, **kwargs): if len(args) > num_allow_args: warnings.warn( msg.format(arguments=_format_argument_list(allow_args)), - FutureWarning, + klass, stacklevel=find_stack_level(), ) return func(*args, **kwargs)