Skip to content

Commit d64cce1

Browse files
MarcoGorelliTLouf
authored andcommitted
Deprecate passing args as positional in sort_index (pandas-dev#41506)
1 parent c6c83e7 commit d64cce1

File tree

7 files changed

+30
-3
lines changed

7 files changed

+30
-3
lines changed

doc/source/user_guide/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ Writing in ISO date format:
18971897
18981898
dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB"))
18991899
dfd["date"] = pd.Timestamp("20130101")
1900-
dfd = dfd.sort_index(1, ascending=False)
1900+
dfd = dfd.sort_index(axis=1, ascending=False)
19011901
json = dfd.to_json(date_format="iso")
19021902
json
19031903

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ Deprecations
680680
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
681681
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
682682
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
683+
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` (:issue:`41485`)
683684
- Deprecated passing arguments as positional in :meth:`DataFrame.drop_duplicates` (except for ``subset``), :meth:`Series.drop_duplicates`, :meth:`Index.drop_duplicates` and :meth:`MultiIndex.drop_duplicates`(:issue:`41485`)
684685
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
685686
- Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`)

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -6292,6 +6292,7 @@ def sort_values( # type: ignore[override]
62926292
else:
62936293
return result.__finalize__(self, method="sort_values")
62946294

6295+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
62956296
def sort_index(
62966297
self,
62976298
axis: Axis = 0,

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -3468,6 +3468,7 @@ def sort_values(
34683468
else:
34693469
return result.__finalize__(self, method="sort_values")
34703470

3471+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
34713472
def sort_index(
34723473
self,
34733474
axis=0,

pandas/tests/frame/methods/test_sort_index.py

+12
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,15 @@ 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"In a future version of pandas all arguments of DataFrame.sort_index "
876+
r"will be keyword-only"
877+
)
878+
with tm.assert_produces_warning(FutureWarning, match=msg):
879+
result = df.sort_index(1)
880+
expected = DataFrame({"a": [1, 2, 3]})
881+
tm.assert_frame_equal(result, expected)

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

+12
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,15 @@ 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"In a future version of pandas all arguments of Series.sort_index "
329+
r"will be keyword-only"
330+
)
331+
with tm.assert_produces_warning(FutureWarning, match=msg):
332+
result = ser.sort_index(0)
333+
expected = Series([1, 2, 3])
334+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)