Skip to content

Commit 18865cf

Browse files
authored
DEPR: Enforce disallowing 2 positional args in DataFrame.reindex (#50612)
1 parent 1e4e624 commit 18865cf

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ Removal of prior version deprecations/changes
628628
- Disallow passing non-keyword arguments to :meth:`DataFrame.replace`, :meth:`Series.replace` except for ``to_replace`` and ``value`` (:issue:`47587`)
629629
- Disallow passing non-keyword arguments to :meth:`DataFrame.sort_values` except for ``by`` (:issue:`41505`)
630630
- Disallow passing non-keyword arguments to :meth:`Series.sort_values` (:issue:`41505`)
631+
- Disallow passing 2 non-keyword arguments to :meth:`DataFrame.reindex` (:issue:`17966`)
631632
- Disallow :meth:`Index.reindex` with non-unique :class:`Index` objects (:issue:`42568`)
632633
- Disallowed constructing :class:`Categorical` with scalar ``data`` (:issue:`38433`)
633634
- Disallowed constructing :class:`CategoricalIndex` without passing ``data`` (:issue:`38944`)

pandas/tests/frame/methods/test_reindex.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,12 @@ def test_reindex_axis_style(self):
840840
result = df.reindex([0, 1, 3], axis="index")
841841
tm.assert_frame_equal(result, expected)
842842

843-
def test_reindex_positional_warns(self):
843+
def test_reindex_positional_raises(self):
844844
# https://github.com/pandas-dev/pandas/issues/12392
845+
# Enforced in 2.0
845846
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
846-
expected = DataFrame({"A": [1.0, 2], "B": [4.0, 5], "C": [np.nan, np.nan]})
847-
with tm.assert_produces_warning(FutureWarning):
848-
result = df.reindex([0, 1], ["A", "B", "C"])
849-
850-
tm.assert_frame_equal(result, expected)
847+
with pytest.raises(TypeError, match=r".* is ambiguous."):
848+
df.reindex([0, 1], ["A", "B", "C"])
851849

852850
def test_reindex_axis_style_raises(self):
853851
# https://github.com/pandas-dev/pandas/issues/12392
@@ -914,9 +912,7 @@ def test_reindex_api_equivalence(self):
914912
for res in [res2, res3]:
915913
tm.assert_frame_equal(res1, res)
916914

917-
with tm.assert_produces_warning(FutureWarning) as m:
918-
res1 = df.reindex(["b", "a"], ["e", "d"])
919-
assert "reindex" in str(m[0].message)
915+
res1 = df.reindex(index=["b", "a"], columns=["e", "d"])
920916
res2 = df.reindex(columns=["e", "d"], index=["b", "a"])
921917
res3 = df.reindex(labels=["b", "a"], axis=0).reindex(labels=["e", "d"], axis=1)
922918
for res in [res2, res3]:

pandas/util/_validators.py

+3-16
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
TypeVar,
1212
overload,
1313
)
14-
import warnings
1514

1615
import numpy as np
1716

18-
from pandas.util._exceptions import find_stack_level
19-
2017
from pandas.core.dtypes.common import (
2118
is_bool,
2219
is_integer,
@@ -297,11 +294,6 @@ def validate_axis_style_args(
297294
>>> validate_axis_style_args(df, (str.upper,), {'columns': id},
298295
... 'mapper', 'rename')
299296
{'columns': <built-in function id>, 'index': <method 'upper' of 'str' objects>}
300-
301-
This emits a warning
302-
>>> validate_axis_style_args(df, (str.upper, id), {},
303-
... 'mapper', 'rename')
304-
{'index': <method 'upper' of 'str' objects>, 'columns': <built-in function id>}
305297
"""
306298
# TODO: Change to keyword-only args and remove all this
307299

@@ -348,15 +340,10 @@ def validate_axis_style_args(
348340
raise TypeError(msg)
349341

350342
msg = (
351-
f"Interpreting call\n\t'.{method_name}(a, b)' as "
352-
f"\n\t'.{method_name}(index=a, columns=b)'.\nUse named "
353-
"arguments to remove any ambiguity. In the future, using "
354-
"positional arguments for 'index' or 'columns' will raise "
355-
"a 'TypeError'."
343+
f"'.{method_name}(a, b)' is ambiguous. Use named keyword arguments"
344+
"for 'index' or 'columns'."
356345
)
357-
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
358-
out[data._get_axis_name(0)] = args[0]
359-
out[data._get_axis_name(1)] = args[1]
346+
raise TypeError(msg)
360347
else:
361348
msg = f"Cannot specify all of '{arg_name}', 'index', 'columns'."
362349
raise TypeError(msg)

0 commit comments

Comments
 (0)