Skip to content

Commit eb149ce

Browse files
EricCheajreback
authored andcommitted
DEPR: Add deprecation warning for factorize() order keyword (#19751)
1 parent 695614d commit eb149ce

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ Deprecations
610610

611611
- The ``broadcast`` parameter of ``.apply()`` is deprecated in favor of ``result_type='broadcast'`` (:issue:`18577`)
612612
- The ``reduce`` parameter of ``.apply()`` is deprecated in favor of ``result_type='reduce'`` (:issue:`18577`)
613+
- The ``order`` parameter of :func:`factorize` is deprecated and will be removed in a future release (:issue:`19727`)
613614

614615
.. _whatsnew_0230.prior_deprecations:
615616

pandas/core/algorithms.py

Lines changed: 2 additions & 0 deletions
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/test_algos.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ def test_uint64_factorize(self):
248248
tm.assert_numpy_array_equal(labels, exp_labels)
249249
tm.assert_numpy_array_equal(uniques, exp_uniques)
250250

251+
def test_deprecate_order(self):
252+
# gh 19727 - check warning is raised for deprecated keyword, order.
253+
# Test not valid once order keyword is removed.
254+
data = np.array([2**63, 1, 2**63], dtype=np.uint64)
255+
with tm.assert_produces_warning(expected_warning=FutureWarning):
256+
algos.factorize(data, order=True)
257+
with tm.assert_produces_warning(False):
258+
algos.factorize(data)
259+
251260

252261
class TestUnique(object):
253262

pandas/tests/util/test_util.py

Lines changed: 14 additions & 0 deletions
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

Lines changed: 36 additions & 2 deletions
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 or 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;
@@ -82,12 +83,15 @@ def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
8283
...
8384
>>> f(columns='should work ok')
8485
should work ok
86+
8587
>>> f(cols='should raise warning')
8688
FutureWarning: cols is deprecated, use columns instead
8789
warnings.warn(msg, FutureWarning)
8890
should raise warning
91+
8992
>>> f(cols='should error', columns="can\'t pass do both")
9093
TypeError: Can only specify 'cols' or 'columns', not both
94+
9195
>>> @deprecate_kwarg('old', 'new', {'yes': True, 'no': False})
9296
... def f(new=False):
9397
... print('yes!' if new else 'no!')
@@ -96,6 +100,25 @@ def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
96100
FutureWarning: old='yes' is deprecated, use new=True instead
97101
warnings.warn(msg, FutureWarning)
98102
yes!
103+
104+
105+
To raise a warning that a keyword will be removed entirely in the future
106+
107+
>>> @deprecate_kwarg(old_arg_name='cols', new_arg_name=None)
108+
... def f(cols='', another_param=''):
109+
... print(cols)
110+
...
111+
>>> f(cols='should raise warning')
112+
FutureWarning: the 'cols' keyword is deprecated and will be removed in a
113+
future version please takes steps to stop use of 'cols'
114+
should raise warning
115+
>>> f(another_param='should not raise warning')
116+
should not raise warning
117+
118+
>>> f(cols='should raise warning', another_param='')
119+
FutureWarning: the 'cols' keyword is deprecated and will be removed in a
120+
future version please takes steps to stop use of 'cols'
121+
should raise warning
99122
"""
100123

101124
if mapping is not None and not hasattr(mapping, 'get') and \
@@ -107,6 +130,17 @@ def _deprecate_kwarg(func):
107130
@wraps(func)
108131
def wrapper(*args, **kwargs):
109132
old_arg_value = kwargs.pop(old_arg_name, None)
133+
134+
if new_arg_name is None and old_arg_value is not None:
135+
msg = (
136+
"the '{old_name}' keyword is deprecated and will be "
137+
"removed in a future version "
138+
"please takes steps to stop use of '{old_name}'"
139+
).format(old_name=old_arg_name)
140+
warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
141+
kwargs[old_arg_name] = old_arg_value
142+
return func(*args, **kwargs)
143+
110144
if old_arg_value is not None:
111145
if mapping is not None:
112146
if hasattr(mapping, 'get'):

0 commit comments

Comments
 (0)