Skip to content

Commit 638ca5b

Browse files
committed
Refinements
1 parent cac0126 commit 638ca5b

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ Enhancement2
2626

2727
New Deprecation Policy
2828
^^^^^^^^^^^^^^^^^^^^^^
29-
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.
29+
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 <https://pandas.pydata.org/pdeps/0017-backwards-compatibility-and-deprecation-policy.html>`_ for more details.
3030

31-
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.
31+
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.
32+
33+
- :class:`pandas.errors.Pandas4Warning`: Warnings which will be enforced in pandas 4.0.
34+
- :class:`pandas.errors.Pandas4Warning`: Warnings which will be enforced in pandas 5.0.
35+
- :class:`pandas.errors.PandasPendingDeprecationWarning`: Warnings which will emit a ``PendingDeprecationWarning``, independent of the version they will be enforced.
36+
- :class:`pandas.errors.PandasDeprecationWarning`: Warnings which will emit a ``DeprecationWarning``, independent of the version they will be enforced.
37+
- :class:`pandas.errors.PandasFutureWarning`: Warnings which will emit a ``PandasFutureWarning``, independent of the version they will be enforced.
3238

3339
.. _whatsnew_300.enhancements.other:
3440

pandas/errors/__init__.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
import abc
78
import ctypes
89

910
from pandas._config.config import OptionError
@@ -93,18 +94,24 @@ class PerformanceWarning(Warning):
9394

9495
class PandasChangeWarning(Warning):
9596
"""
96-
Warning raised for any an upcoming change.
97+
Warning raised for any upcoming change.
9798
9899
See Also
99100
--------
100101
errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0.
102+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
101103
102104
Examples
103105
--------
104106
>>> pd.errors.PandasChangeWarning
105107
<class 'pandas.errors.PandasChangeWarning'>
106108
"""
107109

110+
@classmethod
111+
@abc.abstractmethod
112+
def version(cls) -> str:
113+
"""Version where change will be enforced."""
114+
108115

109116
class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning):
110117
"""
@@ -113,6 +120,7 @@ class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWar
113120
See Also
114121
--------
115122
errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0.
123+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
116124
117125
Examples
118126
--------
@@ -128,6 +136,7 @@ class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning):
128136
See Also
129137
--------
130138
errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0.
139+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
131140
132141
Examples
133142
--------
@@ -157,14 +166,19 @@ class Pandas4Warning(PandasDeprecationWarning):
157166
158167
See Also
159168
--------
160-
errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0.
169+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
161170
162171
Examples
163172
--------
164173
>>> pd.errors.Pandas4Warning
165174
<class 'pandas.errors.Pandas4Warning'>
166175
"""
167176

177+
@classmethod
178+
def version(cls) -> str:
179+
"""Version where change will be enforced."""
180+
return "4.0"
181+
168182

169183
class Pandas5Warning(PandasPendingDeprecationWarning):
170184
"""
@@ -173,13 +187,22 @@ class Pandas5Warning(PandasPendingDeprecationWarning):
173187
See Also
174188
--------
175189
errors.Pandas4Warning : Class for deprecations to be enforced in pandas 4.0.
190+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
176191
177192
Examples
178193
--------
179194
>>> pd.errors.Pandas5Warning
180195
<class 'pandas.errors.Pandas5Warning'>
181196
"""
182197

198+
@classmethod
199+
def version(cls) -> str:
200+
"""Version where change will be enforced."""
201+
return "5.0"
202+
203+
204+
_CurrentDeprecationWarning = Pandas4Warning
205+
183206

184207
class UnsupportedFunctionCall(ValueError):
185208
"""

pandas/tests/api/test_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ def test_api_executors(self):
367367

368368
class TestErrors(Base):
369369
def test_errors(self):
370-
self.check(pd.errors, pd.errors.__all__, ignored=["ctypes", "cow"])
370+
ignored = ["abstractmethod", "ctypes", "cow"]
371+
self.check(pd.errors, pd.errors.__all__, ignored=ignored)
371372

372373

373374
class TestUtil(Base):

pandas/tests/groupby/test_groupby_subclass.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.errors import Pandas4Warning
7+
68
from pandas import (
79
DataFrame,
810
Index,
@@ -36,7 +38,7 @@ def test_groupby_preserves_subclass(obj, groupby_func):
3638

3739
args = get_groupby_method_args(groupby_func, obj)
3840

39-
warn = FutureWarning if groupby_func == "corrwith" else None
41+
warn = Pandas4Warning if groupby_func == "corrwith" else None
4042
msg = f"{type(grouped).__name__}.corrwith is deprecated"
4143
with tm.assert_produces_warning(warn, match=msg):
4244
result1 = getattr(grouped, groupby_func)(*args)

pandas/tests/util/test_deprecate_nonkeyword_arguments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import inspect
66

7-
from pandas.errors import Pandas4Warning
7+
from pandas.errors import _CurrentDeprecationWarning
88
from pandas.util._decorators import deprecate_nonkeyword_arguments
99

1010
import pandas._testing as tm
1111

12-
WARNING_CATEGORY = Pandas4Warning
12+
WARNING_CATEGORY = _CurrentDeprecationWarning
1313

1414

1515
@deprecate_nonkeyword_arguments(

pandas/util/_decorators.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
Mapping,
2424
)
2525

26+
from pandas.errors import PandasChangeWarning
27+
2628

2729
def deprecate(
2830
klass: type[Warning],
@@ -267,7 +269,7 @@ def future_version_msg(version: str | None) -> str:
267269

268270

269271
def deprecate_nonkeyword_arguments(
270-
klass: type[Warning],
272+
klass: type[PandasChangeWarning],
271273
allowed_args: list[str] | None = None,
272274
name: str | None = None,
273275
) -> Callable[[F], F]:
@@ -289,17 +291,6 @@ def deprecate_nonkeyword_arguments(
289291
message. If None, then the Qualified name of the function
290292
is used.
291293
"""
292-
from pandas.errors import (
293-
Pandas4Warning,
294-
Pandas5Warning,
295-
)
296-
297-
if klass is Pandas4Warning:
298-
version = "4.0"
299-
elif klass is Pandas5Warning:
300-
version = "5.0"
301-
else:
302-
raise AssertionError(f"{type(klass)=} must be a versioned warning")
303294

304295
def decorate(func):
305296
old_sig = inspect.signature(func)
@@ -328,7 +319,7 @@ def decorate(func):
328319

329320
num_allow_args = len(allow_args)
330321
msg = (
331-
f"{future_version_msg(version)} all arguments of "
322+
f"{future_version_msg(klass.version())} all arguments of "
332323
f"{name or func.__qualname__}{{arguments}} will be keyword-only."
333324
)
334325

0 commit comments

Comments
 (0)