From 80edcb3dc568c123df7852ed92380842b89aad25 Mon Sep 17 00:00:00 2001 From: Abdulaziz Aloqeely <52792999+DAzVise@users.noreply.github.com> Date: Sat, 6 Apr 2024 00:33:14 +0300 Subject: [PATCH 1/8] ENH: Implement PDEP-17 --- doc/source/reference/testing.rst | 6 ++ doc/source/whatsnew/v3.0.0.rst | 6 ++ pandas/_libs/tslibs/timestamps.pyx | 8 ++- pandas/core/frame.py | 49 +++++++++++---- pandas/core/generic.py | 9 +-- pandas/core/groupby/generic.py | 7 ++- pandas/core/indexes/accessors.py | 5 +- pandas/core/internals/api.py | 3 +- pandas/core/reshape/concat.py | 3 +- pandas/core/series.py | 59 ++++++++++++++----- pandas/errors/__init__.py | 36 +++++++++++ pandas/io/feather_format.py | 3 +- pandas/io/parquet.py | 9 ++- pandas/io/parsers/arrow_parser_wrapper.py | 7 ++- .../tests/copy_view/test_copy_deprecation.py | 20 ++++--- pandas/tests/extension/test_arrow.py | 5 +- .../tests/frame/methods/test_reindex_like.py | 6 +- pandas/tests/groupby/test_all_methods.py | 6 +- pandas/tests/groupby/test_apply.py | 4 +- pandas/tests/groupby/test_categorical.py | 8 ++- pandas/tests/groupby/test_groupby_dropna.py | 5 +- pandas/tests/groupby/test_numeric_only.py | 5 +- pandas/tests/groupby/test_raises.py | 27 ++++++--- .../tests/groupby/transform/test_transform.py | 5 +- pandas/tests/io/formats/test_to_markdown.py | 4 +- pandas/tests/io/formats/test_to_string.py | 4 +- .../tests/io/json/test_json_table_schema.py | 4 +- pandas/tests/io/json/test_pandas.py | 43 +++++++------- pandas/tests/io/test_gcs.py | 3 +- pandas/tests/resample/test_datetime_index.py | 3 +- .../scalar/timestamp/test_constructors.py | 9 ++- .../tests/scalar/timestamp/test_timestamp.py | 7 ++- .../series/accessors/test_cat_accessor.py | 4 +- .../series/accessors/test_dt_accessor.py | 3 +- .../tests/series/methods/test_reindex_like.py | 10 ++-- pandas/tests/tools/test_to_datetime.py | 4 +- pandas/tests/util/test_deprecate.py | 10 +++- pandas/tests/util/test_deprecate_kwarg.py | 8 +-- .../test_deprecate_nonkeyword_arguments.py | 17 ++++-- .../util/test_pandas_deprecation_warning.py | 25 ++++++++ pandas/util/_decorators.py | 26 +++++--- 41 files changed, 350 insertions(+), 135 deletions(-) create mode 100644 pandas/tests/util/test_pandas_deprecation_warning.py 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..c146de59da862 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,9 @@ def all( **kwargs, ) -> Series | bool: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="all") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="all" + ) @doc(make_doc("all", ndim=1)) def all( self, @@ -11956,7 +11959,9 @@ def min( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="min") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="min" + ) @doc(make_doc("min", ndim=2)) def min( self, @@ -12003,7 +12008,9 @@ def max( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="max") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="max" + ) @doc(make_doc("max", ndim=2)) def max( self, @@ -12019,7 +12026,9 @@ def max( result = result.__finalize__(self, method="max") return result - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="sum") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sum" + ) def sum( self, axis: Axis | None = 0, @@ -12120,7 +12129,9 @@ def sum( result = result.__finalize__(self, method="sum") return result - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="prod") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="prod" + ) def prod( self, axis: Axis | None = 0, @@ -12238,7 +12249,9 @@ def mean( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="mean") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="mean" + ) @doc(make_doc("mean", ndim=2)) def mean( self, @@ -12285,7 +12298,9 @@ def median( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="median") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="median" + ) @doc(make_doc("median", ndim=2)) def median( self, @@ -12335,7 +12350,9 @@ def sem( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="sem") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sem" + ) def sem( self, axis: Axis | None = 0, @@ -12455,7 +12472,9 @@ def var( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="var") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="var" + ) def var( self, axis: Axis | None = 0, @@ -12574,7 +12593,9 @@ def std( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="std") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="std" + ) def std( self, axis: Axis | None = 0, @@ -12697,7 +12718,9 @@ def skew( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="skew") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="skew" + ) def skew( self, axis: Axis | None = 0, @@ -12817,7 +12840,9 @@ def kurt( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="kurt") + @deprecate_nonkeyword_arguments( + version="4.0", klass=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..a6b8bd2983ff5 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("method", klass=Pandas4Warning, 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..548b898438c51 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,10 @@ def to_string( ) -> None: ... @deprecate_nonkeyword_arguments( - version="4.0", allowed_args=["self", "buf"], name="to_string" + version="4.0", + klass=Pandas4Warning, + allowed_args=["self", "buf"], + name="to_string", ) def to_string( self, @@ -1630,7 +1634,10 @@ def to_markdown( ), ) @deprecate_nonkeyword_arguments( - version="4.0", allowed_args=["self", "buf"], name="to_markdown" + version="4.0", + klass=Pandas4Warning, + allowed_args=["self", "buf"], + name="to_markdown", ) def to_markdown( self, @@ -6643,7 +6650,9 @@ def any( # type: ignore[override] filter_type="bool", ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="all") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="all" + ) @Appender(make_doc("all", ndim=1)) def all( self, @@ -6663,7 +6672,9 @@ def all( filter_type="bool", ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="min") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="min" + ) def min( self, axis: Axis | None = 0, @@ -6734,7 +6745,9 @@ 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( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="max" + ) def max( self, axis: Axis | None = 0, @@ -6805,7 +6818,9 @@ 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( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sum" + ) def sum( self, axis: Axis | None = None, @@ -6906,7 +6921,9 @@ def sum( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="prod") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="prod" + ) @doc(make_doc("prod", ndim=1)) def prod( self, @@ -6925,7 +6942,9 @@ def prod( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="mean") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="mean" + ) def mean( self, axis: Axis | None = 0, @@ -6979,7 +6998,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( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="median" + ) def median( self, axis: Axis | None = 0, @@ -7060,7 +7081,9 @@ 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( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sem" + ) @doc(make_doc("sem", ndim=1)) def sem( self, @@ -7079,7 +7102,9 @@ def sem( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="var") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="var" + ) def var( self, axis: Axis | None = None, @@ -7166,7 +7191,9 @@ def var( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="std") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="std" + ) @doc(make_doc("std", ndim=1)) def std( self, @@ -7185,7 +7212,9 @@ def std( **kwargs, ) - @deprecate_nonkeyword_arguments(version="4.0", allowed_args=["self"], name="skew") + @deprecate_nonkeyword_arguments( + version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="skew" + ) @doc(make_doc("skew", ndim=1)) def skew( self, @@ -7198,7 +7227,9 @@ 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( + version="4.0", klass=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..1179e5d37f414 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -91,6 +91,42 @@ class PerformanceWarning(Warning): """ +class PandasChangeWarning(Warning): + """ + Warning raised for any pending deprecation. + """ + + +class Pandas4Warning(PandasChangeWarning, DeprecationWarning): + """ + Warning raised for a pending deprecation that will be enforced in pandas 4.0. + """ + + +class Pandas5Warning(PandasChangeWarning, PendingDeprecationWarning): + """ + Warning raised for a pending deprecation that will be enforced in pandas 5.0. + """ + + +class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning): + """ + Warning raised for a pending deprecation that is a PendingDeprecationWarning. + """ + + +class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning): + """ + Warning raised for a pending deprecation that is a DeprecationWarning. + """ + + +class PandasFutureWarning(PandasChangeWarning, FutureWarning): + """ + Warning raised for a pending deprecation that is a FutureWarning. + """ + + class UnsupportedFunctionCall(ValueError): """ Exception raised when attempting to call a unsupported numpy function. 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/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..bdbf2ca2d028d 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( + "depr_func", new_func, "1.0", msg="Use new_func instead.", klass=FutureWarning + ) 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." + "depr_func", + new_func_no_docstring, + "1.0", + msg="Use new_func instead.", + klass=FutureWarning, ) with tm.assert_produces_warning(FutureWarning): result = depr_func() diff --git a/pandas/tests/util/test_deprecate_kwarg.py b/pandas/tests/util/test_deprecate_kwarg.py index b165e9fba0e4f..bbd37a379ede1 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("old", new_arg_name="new", klass=FutureWarning) 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("old", new_arg_name="new", mapping=_f2_mappings, klass=FutureWarning) 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("old", new_arg_name="new", mapping=_f3_mapping, klass=FutureWarning) def _f3(new=0): return new @@ -70,7 +70,7 @@ def f4(new=None): return new -@deprecate_kwarg("old", None) +@deprecate_kwarg("old", new_arg_name=None, klass=FutureWarning) 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..ae2bd0b487019 100644 --- a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py +++ b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py @@ -10,7 +10,7 @@ @deprecate_nonkeyword_arguments( - version="1.1", allowed_args=["a", "b"], name="f_add_inputs" + version="1.1", allowed_args=["a", "b"], name="f_add_inputs", klass=FutureWarning ) def f(a, b=0, c=0, d=0): return a + b + c + d @@ -59,7 +59,7 @@ def test_three_arguments_with_name_in_warning(): assert f(6, 3, 3) == 12 -@deprecate_nonkeyword_arguments(version="1.1") +@deprecate_nonkeyword_arguments(version="1.1", klass=FutureWarning) def g(a, b=0, c=0, d=0): with tm.assert_produces_warning(None): return a + b + c + d @@ -88,7 +88,7 @@ def test_three_positional_argument_with_warning_message_analysis(): assert g(6, 3, 3) == 12 -@deprecate_nonkeyword_arguments(version="1.1") +@deprecate_nonkeyword_arguments(version="1.1", klass=FutureWarning) def h(a=0, b=0, c=0, d=0): return a + b + c + d @@ -113,7 +113,7 @@ def test_one_positional_argument_with_warning_message_analysis(): assert h(19) == 19 -@deprecate_nonkeyword_arguments(version="1.1") +@deprecate_nonkeyword_arguments(version="1.1", klass=UserWarning) def i(a=0, /, b=0, *, c=0, d=0): return a + b + c + d @@ -122,8 +122,15 @@ 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(UserWarning): + assert i(1, 2) == 3 + + class Foo: - @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "bar"]) + @deprecate_nonkeyword_arguments( + version=None, allowed_args=["self", "bar"], klass=FutureWarning + ) def baz(self, bar=None, foobar=None): ... 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..55b132f843adc --- /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("old", klass=PandasChangeWarning, 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..120ad166004c6 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -28,8 +28,8 @@ def deprecate( name: str, alternative: Callable[..., Any], version: str, + klass: type[Warning], alt_name: str | None = None, - klass: type[Warning] | None = None, stacklevel: int = 2, msg: str | None = None, ) -> Callable[[F], F]: @@ -52,14 +52,14 @@ 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 + klass : Warning, optional + The warning class to use. 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) @@ -101,6 +101,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: def deprecate_kwarg( old_arg_name: str, + klass: type[Warning], new_arg_name: str | None, mapping: Mapping[Any, Any] | Callable[[Any], Any] | None = None, stacklevel: int = 2, @@ -111,7 +112,7 @@ def deprecate_kwarg( Parameters ---------- 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,6 +120,9 @@ 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. + klass : Warning, optional + The warning class to use. + stacklevel : int, default 2 Examples -------- @@ -153,14 +157,14 @@ def deprecate_kwarg( ... 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} " @@ -264,6 +268,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]: @@ -288,6 +293,9 @@ def deprecate_nonkeyword_arguments( The specific name of the function to show in the warning message. If None, then the Qualified name of the function is used. + + klass : Warning, optional + The warning class to use. """ def decorate(func): @@ -326,7 +334,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) From 054398dfb6f50e4a2ffb28a5f337cbdc6af004e9 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Tue, 20 May 2025 18:30:26 -0400 Subject: [PATCH 2/8] Refinements --- pandas/errors/__init__.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 1179e5d37f414..2ed1cc65d3c68 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -93,37 +93,37 @@ class PerformanceWarning(Warning): class PandasChangeWarning(Warning): """ - Warning raised for any pending deprecation. + Warning raised for any an upcoming change. """ -class Pandas4Warning(PandasChangeWarning, DeprecationWarning): +class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning): """ - Warning raised for a pending deprecation that will be enforced in pandas 4.0. + Warning raised for an upcoming change that is a PendingDeprecationWarning. """ -class Pandas5Warning(PandasChangeWarning, PendingDeprecationWarning): +class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning): """ - Warning raised for a pending deprecation that will be enforced in pandas 5.0. + Warning raised for an upcoming change that is a DeprecationWarning. """ -class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning): +class PandasFutureWarning(PandasChangeWarning, FutureWarning): """ - Warning raised for a pending deprecation that is a PendingDeprecationWarning. + Warning raised for an upcoming change that is a FutureWarning. """ -class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning): +class Pandas4Warning(PandasDeprecationWarning): """ - Warning raised for a pending deprecation that is a DeprecationWarning. + Warning raised for an upcoming change that will be enforced in pandas 4.0. """ -class PandasFutureWarning(PandasChangeWarning, FutureWarning): +class Pandas5Warning(PandasPendingDeprecationWarning): """ - Warning raised for a pending deprecation that is a FutureWarning. + Warning raised for an upcoming change that will be enforced in pandas 5.0. """ From 4e46d69f0b26d3208d5b61266d651b728bb2bb07 Mon Sep 17 00:00:00 2001 From: richard Date: Wed, 21 May 2025 00:00:21 -0400 Subject: [PATCH 3/8] Refinements --- pandas/core/frame.py | 46 ++++++---------------- pandas/core/generic.py | 2 +- pandas/core/series.py | 56 +++++++-------------------- pandas/errors/__init__.py | 60 +++++++++++++++++++++++++++++ pandas/tests/test_errors.py | 25 ++++++++++++ pandas/tests/util/test_deprecate.py | 10 +++-- pandas/util/_decorators.py | 41 +++++++++++--------- 7 files changed, 141 insertions(+), 99 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c146de59da862..b7bd526c37493 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11910,9 +11910,7 @@ def all( **kwargs, ) -> Series | bool: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="all" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="all") @doc(make_doc("all", ndim=1)) def all( self, @@ -11959,9 +11957,7 @@ def min( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="min" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="min") @doc(make_doc("min", ndim=2)) def min( self, @@ -12008,9 +12004,7 @@ def max( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="max" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="max") @doc(make_doc("max", ndim=2)) def max( self, @@ -12026,9 +12020,7 @@ def max( result = result.__finalize__(self, method="max") return result - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sum" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sum") def sum( self, axis: Axis | None = 0, @@ -12129,9 +12121,7 @@ def sum( result = result.__finalize__(self, method="sum") return result - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="prod" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="prod") def prod( self, axis: Axis | None = 0, @@ -12249,9 +12239,7 @@ def mean( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="mean" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="mean") @doc(make_doc("mean", ndim=2)) def mean( self, @@ -12299,7 +12287,7 @@ def median( ) -> Series | Any: ... @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="median" + Pandas4Warning, allowed_args=["self"], name="median" ) @doc(make_doc("median", ndim=2)) def median( @@ -12350,9 +12338,7 @@ def sem( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sem" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sem") def sem( self, axis: Axis | None = 0, @@ -12472,9 +12458,7 @@ def var( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="var" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="var") def var( self, axis: Axis | None = 0, @@ -12593,9 +12577,7 @@ def std( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="std" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="std") def std( self, axis: Axis | None = 0, @@ -12718,9 +12700,7 @@ def skew( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="skew" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="skew") def skew( self, axis: Axis | None = 0, @@ -12840,9 +12820,7 @@ def kurt( **kwargs, ) -> Series | Any: ... - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, 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 a6b8bd2983ff5..e72fd92236969 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4387,7 +4387,7 @@ def _check_copy_deprecation(copy): ) # issue 58667 - @deprecate_kwarg("method", klass=Pandas4Warning, new_arg_name=None) + @deprecate_kwarg(Pandas4Warning, "method", new_arg_name=None) @final def reindex_like( self, diff --git a/pandas/core/series.py b/pandas/core/series.py index 548b898438c51..f98e8c2563abb 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1473,10 +1473,7 @@ def to_string( ) -> None: ... @deprecate_nonkeyword_arguments( - version="4.0", - klass=Pandas4Warning, - allowed_args=["self", "buf"], - name="to_string", + Pandas4Warning, allowed_args=["self", "buf"], name="to_string" ) def to_string( self, @@ -1634,10 +1631,7 @@ def to_markdown( ), ) @deprecate_nonkeyword_arguments( - version="4.0", - klass=Pandas4Warning, - allowed_args=["self", "buf"], - name="to_markdown", + Pandas4Warning, allowed_args=["self", "buf"], name="to_markdown" ) def to_markdown( self, @@ -6650,9 +6644,7 @@ def any( # type: ignore[override] filter_type="bool", ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="all" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="all") @Appender(make_doc("all", ndim=1)) def all( self, @@ -6672,9 +6664,7 @@ def all( filter_type="bool", ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="min" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="min") def min( self, axis: Axis | None = 0, @@ -6745,9 +6735,7 @@ def min( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="max" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="max") def max( self, axis: Axis | None = 0, @@ -6818,9 +6806,7 @@ def max( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sum" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sum") def sum( self, axis: Axis | None = None, @@ -6921,9 +6907,7 @@ def sum( **kwargs, ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="prod" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="prod") @doc(make_doc("prod", ndim=1)) def prod( self, @@ -6942,9 +6926,7 @@ def prod( **kwargs, ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="mean" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="mean") def mean( self, axis: Axis | None = 0, @@ -6999,7 +6981,7 @@ def mean( ) @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="median" + Pandas4Warning, allowed_args=["self"], name="median" ) def median( self, @@ -7081,9 +7063,7 @@ def median( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="sem" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="sem") @doc(make_doc("sem", ndim=1)) def sem( self, @@ -7102,9 +7082,7 @@ def sem( **kwargs, ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="var" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="var") def var( self, axis: Axis | None = None, @@ -7191,9 +7169,7 @@ def var( **kwargs, ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="std" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="std") @doc(make_doc("std", ndim=1)) def std( self, @@ -7212,9 +7188,7 @@ def std( **kwargs, ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, allowed_args=["self"], name="skew" - ) + @deprecate_nonkeyword_arguments(Pandas4Warning, allowed_args=["self"], name="skew") @doc(make_doc("skew", ndim=1)) def skew( self, @@ -7227,9 +7201,7 @@ def skew( self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs ) - @deprecate_nonkeyword_arguments( - version="4.0", klass=Pandas4Warning, 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 2ed1cc65d3c68..57be6a07c464d 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -94,36 +94,90 @@ 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 + """ @@ -968,6 +1022,12 @@ class InvalidComparison(Exception): "OptionError", "OutOfBoundsDatetime", "OutOfBoundsTimedelta", + "Pandas4Warning", + "Pandas5Warning", + "PandasChangeWarning", + "PandasDeprecationWarning", + "PandasFutureWarning", + "PandasPendingDeprecationWarning", "ParserError", "ParserWarning", "PerformanceWarning", 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/util/test_deprecate.py b/pandas/tests/util/test_deprecate.py index bdbf2ca2d028d..94c8fe7fd45d1 100644 --- a/pandas/tests/util/test_deprecate.py +++ b/pandas/tests/util/test_deprecate.py @@ -38,7 +38,7 @@ def new_func_with_deprecation(): def test_deprecate_ok(): depr_func = deprecate( - "depr_func", new_func, "1.0", msg="Use new_func instead.", klass=FutureWarning + FutureWarning, "depr_func", new_func, "1.0", msg="Use new_func instead." ) with tm.assert_produces_warning(FutureWarning): @@ -50,11 +50,11 @@ def test_deprecate_ok(): def test_deprecate_no_docstring(): depr_func = deprecate( + FutureWarning, "depr_func", new_func_no_docstring, "1.0", msg="Use new_func instead.", - klass=FutureWarning, ) with tm.assert_produces_warning(FutureWarning): result = depr_func() @@ -65,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/util/_decorators.py b/pandas/util/_decorators.py index 120ad166004c6..803b608d282bf 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -25,10 +25,10 @@ def deprecate( + klass: type[Warning], name: str, alternative: Callable[..., Any], version: str, - klass: type[Warning], alt_name: str | None = None, stacklevel: int = 2, msg: str | None = None, @@ -44,6 +44,8 @@ def deprecate( Parameters ---------- + klass : Warning + The warning class to use. name : str Name of function to deprecate. alternative : func @@ -52,8 +54,6 @@ 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, optional - The warning class to use. stacklevel : int, default 2 msg : str The message to display in the warning. @@ -100,8 +100,8 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: def deprecate_kwarg( - old_arg_name: str, klass: type[Warning], + old_arg_name: str, new_arg_name: str | None, mapping: Mapping[Any, Any] | Callable[[Any], Any] | None = None, stacklevel: int = 2, @@ -111,6 +111,8 @@ def deprecate_kwarg( Parameters ---------- + klass : Warning + The warning class to use. old_arg_name : str Name of argument in function to deprecate. new_arg_name : str or None @@ -120,15 +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. - klass : Warning, optional - The warning class to use. 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") @@ -142,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 @@ -152,7 +152,7 @@ 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 @@ -267,7 +267,6 @@ 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, @@ -277,26 +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. - - klass : Warning, optional - The warning class to use. """ + 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) From 3f3293f5532ac2973f546c6741203d377139c097 Mon Sep 17 00:00:00 2001 From: richard Date: Wed, 21 May 2025 00:22:31 -0400 Subject: [PATCH 4/8] Fixup --- pandas/tests/util/test_deprecate_kwarg.py | 10 ++--- .../test_deprecate_nonkeyword_arguments.py | 41 ++++++++++--------- .../util/test_pandas_deprecation_warning.py | 2 +- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/pandas/tests/util/test_deprecate_kwarg.py b/pandas/tests/util/test_deprecate_kwarg.py index bbd37a379ede1..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_arg_name="new", klass=FutureWarning) +@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_arg_name="new", mapping=_f2_mappings, klass=FutureWarning) +@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_arg_name="new", mapping=_f3_mapping, klass=FutureWarning) +@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", new_arg_name=None, klass=FutureWarning) +@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 ae2bd0b487019..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", klass=FutureWarning + 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", klass=FutureWarning) +@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", klass=FutureWarning) +@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", klass=UserWarning) +@deprecate_nonkeyword_arguments(WARNING_CATEGORY) def i(a=0, /, b=0, *, c=0, d=0): return a + b + c + d @@ -123,14 +126,12 @@ def test_i_signature(): def test_i_warns_klass(): - with tm.assert_produces_warning(UserWarning): + with tm.assert_produces_warning(WARNING_CATEGORY): assert i(1, 2) == 3 class Foo: - @deprecate_nonkeyword_arguments( - version=None, allowed_args=["self", "bar"], klass=FutureWarning - ) + @deprecate_nonkeyword_arguments(WARNING_CATEGORY, allowed_args=["self", "bar"]) def baz(self, bar=None, foobar=None): ... @@ -140,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 index 55b132f843adc..fe9d04f3ab10d 100644 --- a/pandas/tests/util/test_pandas_deprecation_warning.py +++ b/pandas/tests/util/test_pandas_deprecation_warning.py @@ -15,7 +15,7 @@ def test_function_warns_pandas_deprecation_warning(): f1() -@deprecate_kwarg("old", klass=PandasChangeWarning, new_arg_name="new") +@deprecate_kwarg(PandasChangeWarning, "old", new_arg_name="new") def f2(new=0): return new From 638ca5b46858dbfa8d7718335ad7e2771f850b41 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 2 Jun 2025 06:46:16 -0400 Subject: [PATCH 5/8] Refinements --- doc/source/whatsnew/v3.0.0.rst | 10 +++++-- pandas/errors/__init__.py | 27 +++++++++++++++++-- pandas/tests/api/test_api.py | 3 ++- pandas/tests/groupby/test_groupby_subclass.py | 4 ++- .../test_deprecate_nonkeyword_arguments.py | 4 +-- pandas/util/_decorators.py | 17 +++--------- 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 4511d8a7c8252..d3d0e8aee6a29 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -26,9 +26,15 @@ 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. +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. See `PDEP 17 `_ for more details. -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. +All warnings for upcoming changes in pandas will have the base class :class:`pandas.errors.PandasChangeWarning`. Users may also use the following subclasses to control warnings. + +- :class:`pandas.errors.Pandas4Warning`: Warnings which will be enforced in pandas 4.0. +- :class:`pandas.errors.Pandas4Warning`: Warnings which will be enforced in pandas 5.0. +- :class:`pandas.errors.PandasPendingDeprecationWarning`: Warnings which will emit a ``PendingDeprecationWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasDeprecationWarning`: Warnings which will emit a ``DeprecationWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasFutureWarning`: Warnings which will emit a ``PandasFutureWarning``, independent of the version they will be enforced. .. _whatsnew_300.enhancements.other: diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 57be6a07c464d..726c90b2653b7 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -4,6 +4,7 @@ from __future__ import annotations +import abc import ctypes from pandas._config.config import OptionError @@ -93,11 +94,12 @@ class PerformanceWarning(Warning): class PandasChangeWarning(Warning): """ - Warning raised for any an upcoming change. + Warning raised for any upcoming change. See Also -------- errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples -------- @@ -105,6 +107,11 @@ class PandasChangeWarning(Warning): """ + @classmethod + @abc.abstractmethod + def version(cls) -> str: + """Version where change will be enforced.""" + class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning): """ @@ -113,6 +120,7 @@ class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWar See Also -------- errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples -------- @@ -128,6 +136,7 @@ class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning): See Also -------- errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples -------- @@ -157,7 +166,7 @@ class Pandas4Warning(PandasDeprecationWarning): See Also -------- - errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples -------- @@ -165,6 +174,11 @@ class Pandas4Warning(PandasDeprecationWarning): """ + @classmethod + def version(cls) -> str: + """Version where change will be enforced.""" + return "4.0" + class Pandas5Warning(PandasPendingDeprecationWarning): """ @@ -173,6 +187,7 @@ class Pandas5Warning(PandasPendingDeprecationWarning): See Also -------- errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples -------- @@ -180,6 +195,14 @@ class Pandas5Warning(PandasPendingDeprecationWarning): """ + @classmethod + def version(cls) -> str: + """Version where change will be enforced.""" + return "5.0" + + +_CurrentDeprecationWarning = Pandas4Warning + class UnsupportedFunctionCall(ValueError): """ diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 871e977cbe2f8..b811a62846481 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -367,7 +367,8 @@ def test_api_executors(self): class TestErrors(Base): def test_errors(self): - self.check(pd.errors, pd.errors.__all__, ignored=["ctypes", "cow"]) + ignored = ["abstractmethod", "ctypes", "cow"] + self.check(pd.errors, pd.errors.__all__, ignored=ignored) class TestUtil(Base): diff --git a/pandas/tests/groupby/test_groupby_subclass.py b/pandas/tests/groupby/test_groupby_subclass.py index 3ee9c9ea0c7fd..5ffb3bc147fdf 100644 --- a/pandas/tests/groupby/test_groupby_subclass.py +++ b/pandas/tests/groupby/test_groupby_subclass.py @@ -3,6 +3,8 @@ import numpy as np import pytest +from pandas.errors import Pandas4Warning + from pandas import ( DataFrame, Index, @@ -36,7 +38,7 @@ def test_groupby_preserves_subclass(obj, groupby_func): args = get_groupby_method_args(groupby_func, obj) - warn = FutureWarning if groupby_func == "corrwith" else None + warn = Pandas4Warning if groupby_func == "corrwith" else None msg = f"{type(grouped).__name__}.corrwith is deprecated" with tm.assert_produces_warning(warn, match=msg): result1 = getattr(grouped, groupby_func)(*args) diff --git a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py index 8ca6482096da7..7039a13a447a4 100644 --- a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py +++ b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py @@ -4,12 +4,12 @@ import inspect -from pandas.errors import Pandas4Warning +from pandas.errors import _CurrentDeprecationWarning from pandas.util._decorators import deprecate_nonkeyword_arguments import pandas._testing as tm -WARNING_CATEGORY = Pandas4Warning +WARNING_CATEGORY = _CurrentDeprecationWarning @deprecate_nonkeyword_arguments( diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index 803b608d282bf..981dc0c4197d1 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -23,6 +23,8 @@ Mapping, ) + from pandas.errors import PandasChangeWarning + def deprecate( klass: type[Warning], @@ -267,7 +269,7 @@ def future_version_msg(version: str | None) -> str: def deprecate_nonkeyword_arguments( - klass: type[Warning], + klass: type[PandasChangeWarning], allowed_args: list[str] | None = None, name: str | None = None, ) -> Callable[[F], F]: @@ -289,17 +291,6 @@ def deprecate_nonkeyword_arguments( 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) @@ -328,7 +319,7 @@ def decorate(func): num_allow_args = len(allow_args) msg = ( - f"{future_version_msg(version)} all arguments of " + f"{future_version_msg(klass.version())} all arguments of " f"{name or func.__qualname__}{{arguments}} will be keyword-only." ) From e2960b6cb555e06856bede7820f86b7c22d763bd Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 2 Jun 2025 06:57:25 -0400 Subject: [PATCH 6/8] Add implementation details to PDEP-17 --- doc/source/whatsnew/v3.0.0.rst | 6 +++--- ...0017-backwards-compatibility-and-deprecation-policy.md | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index d3d0e8aee6a29..83f01220077d9 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -32,9 +32,9 @@ All warnings for upcoming changes in pandas will have the base class :class:`pan - :class:`pandas.errors.Pandas4Warning`: Warnings which will be enforced in pandas 4.0. - :class:`pandas.errors.Pandas4Warning`: Warnings which will be enforced in pandas 5.0. -- :class:`pandas.errors.PandasPendingDeprecationWarning`: Warnings which will emit a ``PendingDeprecationWarning``, independent of the version they will be enforced. -- :class:`pandas.errors.PandasDeprecationWarning`: Warnings which will emit a ``DeprecationWarning``, independent of the version they will be enforced. -- :class:`pandas.errors.PandasFutureWarning`: Warnings which will emit a ``PandasFutureWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasPendingDeprecationWarning`: Base class of all warnings which emit a ``PendingDeprecationWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasDeprecationWarning`: Base class of all warnings which emit a ``DeprecationWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasFutureWarning`: Base class of all warnings which emit a ``PandasFutureWarning``, independent of the version they will be enforced. .. _whatsnew_300.enhancements.other: diff --git a/web/pandas/pdeps/0017-backwards-compatibility-and-deprecation-policy.md b/web/pandas/pdeps/0017-backwards-compatibility-and-deprecation-policy.md index b8eba90f399c9..561d774e21261 100644 --- a/web/pandas/pdeps/0017-backwards-compatibility-and-deprecation-policy.md +++ b/web/pandas/pdeps/0017-backwards-compatibility-and-deprecation-policy.md @@ -58,8 +58,12 @@ Additionally, when one introduces a deprecation, they should: ### Which warning class to use -Deprecations should initially use ``DeprecationWarning``, and then be switched to ``FutureWarning`` for broader visibility in the last minor release before the major release they are planned to be removed in. -This implementation detail can be ignored by using the appropriate ``PandasDeprecationWarning`` variable, which will be aliased to the proper warning class based on the pandas version. +Starting in pandas 3.0, pandas will provide version-specific warnings. For example, ``Pandas4Warnings`` for all deprecation warnings that will be enforced in pandas 4.0. In addition to these, pandas exposes four additional classes to give users more control over pandas deprecation warnings. + +- :class:`pandas.errors.PandasChangeWarning`: Base class of all pandas deprecation warnings. +- :class:`pandas.errors.PandasPendingDeprecationWarning`: Base class of all warnings which emit a ``PendingDeprecationWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasDeprecationWarning`: Base class of all warnings which emit a ``DeprecationWarning``, independent of the version they will be enforced. +- :class:`pandas.errors.PandasFutureWarning`: Base class of all warnings which emit a ``PandasFutureWarning``, independent of the version they will be enforced. ### Enforcement of deprecations From 92a4e7e34ab387fe39b3424db7cc1754adf522c3 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 2 Jun 2025 17:12:06 -0400 Subject: [PATCH 7/8] API test fixup --- pandas/tests/api/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index b811a62846481..c2e77b69aadcb 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -367,7 +367,7 @@ def test_api_executors(self): class TestErrors(Base): def test_errors(self): - ignored = ["abstractmethod", "ctypes", "cow"] + ignored = ["_CurrentDeprecationWarning", "abc", "ctypes", "cow"] self.check(pd.errors, pd.errors.__all__, ignored=ignored) From 6bca579939e4005226c8ff45788caef50130c52c Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Tue, 3 Jun 2025 20:42:08 -0400 Subject: [PATCH 8/8] Refinements --- pandas/errors/__init__.py | 30 +++++++++++++++---- pandas/tests/test_errors.py | 15 ++++++++++ .../test_deprecate_nonkeyword_arguments.py | 15 ++++++---- .../util/test_pandas_deprecation_warning.py | 25 ---------------- 4 files changed, 49 insertions(+), 36 deletions(-) delete mode 100644 pandas/tests/util/test_pandas_deprecation_warning.py diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 726c90b2653b7..6097a86498532 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -98,7 +98,10 @@ class PandasChangeWarning(Warning): See Also -------- - errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a + PendingDeprecationWarning. + errors.PandasDeprecationWarning : Class for deprecations that will raise a + DeprecationWarning. errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples @@ -119,7 +122,9 @@ class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWar See Also -------- - errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasChangeWarning: Class for deprecations that will raise any warning. + errors.PandasDeprecationWarning : Class for deprecations that will raise a + DeprecationWarning. errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples @@ -135,7 +140,9 @@ class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning): See Also -------- - errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasChangeWarning: Class for deprecations that will raise any warning. + errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a + PendingDeprecationWarning. errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples @@ -151,7 +158,11 @@ class PandasFutureWarning(PandasChangeWarning, FutureWarning): See Also -------- - errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasChangeWarning: Class for deprecations that will raise any warning. + errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a + PendingDeprecationWarning. + errors.PandasDeprecationWarning : Class for deprecations that will raise a + DeprecationWarning. Examples -------- @@ -166,6 +177,11 @@ class Pandas4Warning(PandasDeprecationWarning): See Also -------- + errors.PandasChangeWarning: Class for deprecations that will raise any warning. + errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a + PendingDeprecationWarning. + errors.PandasDeprecationWarning : Class for deprecations that will raise a + DeprecationWarning. errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples @@ -186,7 +202,11 @@ class Pandas5Warning(PandasPendingDeprecationWarning): See Also -------- - errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0. + errors.PandasChangeWarning: Class for deprecations that will raise any warning. + errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a + PendingDeprecationWarning. + errors.PandasDeprecationWarning : Class for deprecations that will raise a + DeprecationWarning. errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning. Examples diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index 02b784a187f88..a9895e89cbf24 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -112,6 +112,21 @@ def test_AbstractMethodError_classmethod(): Foo().method() +@pytest.mark.parametrize( + "warn_category, catch_category", + [ + (Pandas4Warning, PandasChangeWarning), + (Pandas4Warning, PandasDeprecationWarning), + (Pandas5Warning, PandasChangeWarning), + (Pandas5Warning, PandasPendingDeprecationWarning), + ], +) +def test_pandas_warnings(warn_category, catch_category): + # https://github.com/pandas-dev/pandas/pull/61468 + with tm.assert_produces_warning(catch_category): + warnings.warn("test", category=warn_category) + + @pytest.mark.parametrize( "warn_category, filter_category", [ diff --git a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py index 7039a13a447a4..f9300adffc0d6 100644 --- a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py +++ b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py @@ -55,8 +55,8 @@ def test_four_arguments(): def test_three_arguments_with_name_in_warning(): msg = ( - "Starting with pandas version 4.0 all arguments of f_add_inputs " - "except for the arguments 'a' and 'b' will be keyword-only." + f"Starting with pandas version {WARNING_CATEGORY.version()} all arguments of " + "f_add_inputs except for the arguments 'a' and 'b' will be keyword-only." ) with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): assert f(6, 3, 3) == 12 @@ -84,7 +84,7 @@ def test_three_arguments_default_allowed_args(): def test_three_positional_argument_with_warning_message_analysis(): msg = ( - "Starting with pandas version 4.0 all arguments of g " + f"Starting with pandas version {WARNING_CATEGORY.version()} all arguments of g " "except for the argument 'a' will be keyword-only." ) with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): @@ -111,7 +111,10 @@ def test_one_positional_argument(): def test_one_positional_argument_with_warning_message_analysis(): - msg = "Starting with pandas version 4.0 all arguments of h will be keyword-only." + msg = ( + f"Starting with pandas version {WARNING_CATEGORY.version()} all arguments " + "of h will be keyword-only." + ) with tm.assert_produces_warning(WARNING_CATEGORY, match=msg): assert h(19) == 19 @@ -141,8 +144,8 @@ def test_foo_signature(): def test_class(): msg = ( - r"Starting with pandas version 4.0 all arguments of Foo\.baz " - r"except for the argument \'bar\' will be keyword-only" + rf"Starting with pandas version {WARNING_CATEGORY.version()} all arguments " + r"of Foo\.baz except for the argument \'bar\' will be keyword-only" ) 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 deleted file mode 100644 index fe9d04f3ab10d..0000000000000 --- a/pandas/tests/util/test_pandas_deprecation_warning.py +++ /dev/null @@ -1,25 +0,0 @@ -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)