Skip to content

Commit 261044a

Browse files
author
eric
committed
[Utils][Warnings] Add deprecation warning for factorize() keyword, order. Add deprecation warning for entire keywords to deprecate_kwaargs util.
1 parent c4770c7 commit 261044a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

pandas/core/algorithms.py

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pandas.core import common as com
3333
from pandas._libs import algos, lib, hashtable as htable
3434
from pandas._libs.tslib import iNaT
35+
from pandas.util._decorators import deprecate_kwarg
3536

3637

3738
# --------------- #
@@ -436,6 +437,7 @@ def isin(comps, values):
436437
return f(comps, values)
437438

438439

440+
@deprecate_kwarg(old_arg_name='order', new_arg_name=None)
439441
def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
440442
"""
441443
Encode input values as an enumerated type or categorical variable

pandas/tests/util/test_util.py

+14
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ def _f2(new=False):
3434
def _f3(new=0):
3535
return new
3636

37+
@deprecate_kwarg('old', None)
38+
def _f4(old=True, unchanged=True):
39+
return old
40+
3741
self.f1 = _f1
3842
self.f2 = _f2
3943
self.f3 = _f3
44+
self.f4 = _f4
4045

4146
def test_deprecate_kwarg(self):
4247
x = 78
@@ -72,6 +77,15 @@ def test_bad_deprecate_kwarg(self):
7277
def f4(new=None):
7378
pass
7479

80+
def test_deprecate_keyword(self):
81+
x = 9
82+
with tm.assert_produces_warning(FutureWarning):
83+
result = self.f4(old=x)
84+
assert result is x
85+
with tm.assert_produces_warning(None):
86+
result = self.f4(unchanged=x)
87+
assert result is True
88+
7589

7690
def test_rands():
7791
r = tm.rands(10)

pandas/util/_decorators.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
6565
----------
6666
old_arg_name : str
6767
Name of argument in function to deprecate
68-
new_arg_name : str
69-
Name of preferred argument in function
68+
new_arg_name : str | None
69+
Name of preferred argument in function. Use none to raise warning that
70+
``old_arg_name`` keyword is deprecated.
7071
mapping : dict or callable
7172
If mapping is present, use it to translate old arguments to
7273
new arguments. A callable must do its own value checking;
@@ -107,6 +108,16 @@ def _deprecate_kwarg(func):
107108
@wraps(func)
108109
def wrapper(*args, **kwargs):
109110
old_arg_value = kwargs.pop(old_arg_name, None)
111+
112+
if new_arg_name is None and old_arg_value is not None:
113+
msg = (
114+
"the '{old_name}' keyword is no longer supported"
115+
"please takes steps to stop use of '{old_name}'"
116+
).format(old_name=old_arg_name)
117+
warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
118+
kwargs[old_arg_name] = old_arg_value
119+
return func(*args, **kwargs)
120+
110121
if old_arg_value is not None:
111122
if mapping is not None:
112123
if hasattr(mapping, 'get'):

0 commit comments

Comments
 (0)