Skip to content

Commit 9398712

Browse files
committed
deprecate positional args in sort_index
1 parent b2a36bd commit 9398712

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ Deprecations
647647
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
648648
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
649649
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
650+
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` (:issue:`41485`)
650651

651652
.. ---------------------------------------------------------------------------
652653

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
Appender,
7878
Substitution,
7979
deprecate_kwarg,
80+
deprecate_nonkeyword_arguments,
8081
doc,
8182
rewrite_axis_style_signature,
8283
)
@@ -6262,6 +6263,7 @@ def sort_values( # type: ignore[override]
62626263
else:
62636264
return result.__finalize__(self, method="sort_values")
62646265

6266+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"])
62656267
def sort_index(
62666268
self,
62676269
axis: Axis = 0,

pandas/core/series.py

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from pandas.util._decorators import (
5252
Appender,
5353
Substitution,
54+
deprecate_nonkeyword_arguments,
5455
doc,
5556
)
5657
from pandas.util._validators import (
@@ -3434,6 +3435,7 @@ def sort_values(
34343435
else:
34353436
return result.__finalize__(self, method="sort_values")
34363437

3438+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"])
34373439
def sort_index(
34383440
self,
34393441
axis=0,

pandas/tests/frame/methods/test_sort_index.py

+10
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,13 @@ def test_sort_index_multiindex_sparse_column(self):
867867
result = expected.sort_index(level=0)
868868

869869
tm.assert_frame_equal(result, expected)
870+
871+
def test_sort_index_pos_args_deprecation(self):
872+
# https://github.com/pandas-dev/pandas/issues/41485
873+
df = DataFrame({"a": [1, 2, 3]})
874+
msg = (
875+
r"Starting with Pandas version 2\.0 all arguments of sort_index "
876+
r"except for the argument 'self' will be keyword-only"
877+
)
878+
with tm.assert_produces_warning(FutureWarning, match=msg):
879+
df.sort_index(1)

pandas/tests/indexing/test_loc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1352,9 +1352,9 @@ def test_loc_setitem_unsorted_multiindex_columns(self, key):
13521352
expected = DataFrame([[0, 2, 0], [0, 5, 0]], columns=mi)
13531353
tm.assert_frame_equal(obj, expected)
13541354

1355-
df = df.sort_index(1)
1355+
df = df.sort_index(axis=1)
13561356
df.loc[:, key] = np.zeros((2, 2), dtype=int)
1357-
expected = expected.sort_index(1)
1357+
expected = expected.sort_index(axis=1)
13581358
tm.assert_frame_equal(df, expected)
13591359

13601360
def test_loc_setitem_uint_drop(self, any_int_dtype):

pandas/tests/series/methods/test_sort_index.py

+10
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,13 @@ def test_sort_values_key_type(self):
320320
result = s.sort_index(key=lambda x: x.month_name())
321321
expected = s.iloc[[2, 1, 0]]
322322
tm.assert_series_equal(result, expected)
323+
324+
def test_sort_index_pos_args_deprecation(self):
325+
# https://github.com/pandas-dev/pandas/issues/41485
326+
ser = Series([1, 2, 3])
327+
msg = (
328+
r"Starting with Pandas version 2\.0 all arguments of sort_index "
329+
r"except for the argument 'self' will be keyword-only"
330+
)
331+
with tm.assert_produces_warning(FutureWarning, match=msg):
332+
ser.sort_index(0)

0 commit comments

Comments
 (0)