diff --git a/doc/source/user_guide/io.rst b/doc/source/user_guide/io.rst index d26e511202f9c..953046a3a2a10 100644 --- a/doc/source/user_guide/io.rst +++ b/doc/source/user_guide/io.rst @@ -1897,7 +1897,7 @@ Writing in ISO date format: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) dfd["date"] = pd.Timestamp("20130101") - dfd = dfd.sort_index(1, ascending=False) + dfd = dfd.sort_index(axis=1, ascending=False) json = dfd.to_json(date_format="iso") json diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 3d3161f112327..8b1d92d28776f 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -680,6 +680,7 @@ Deprecations - Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`) - Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`) - Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`) +- Deprecated passing arguments as positional in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` (:issue:`41485`) - 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`) - Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`) - 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`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2ad0b28f370da..6d830a7271ff1 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6292,6 +6292,7 @@ def sort_values( # type: ignore[override] else: return result.__finalize__(self, method="sort_values") + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) def sort_index( self, axis: Axis = 0, diff --git a/pandas/core/series.py b/pandas/core/series.py index 74ba534986592..9f804985a2288 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3468,6 +3468,7 @@ def sort_values( else: return result.__finalize__(self, method="sort_values") + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) def sort_index( self, axis=0, diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index a04ed7e92478f..dbb6bb116828a 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -867,3 +867,15 @@ def test_sort_index_multiindex_sparse_column(self): result = expected.sort_index(level=0) tm.assert_frame_equal(result, expected) + + def test_sort_index_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"In a future version of pandas all arguments of DataFrame.sort_index " + r"will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.sort_index(1) + expected = DataFrame({"a": [1, 2, 3]}) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index b8e7b83c97ddb..8c79bafa2f888 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1352,9 +1352,9 @@ def test_loc_setitem_unsorted_multiindex_columns(self, key): expected = DataFrame([[0, 2, 0], [0, 5, 0]], columns=mi) tm.assert_frame_equal(obj, expected) - df = df.sort_index(1) + df = df.sort_index(axis=1) df.loc[:, key] = np.zeros((2, 2), dtype=int) - expected = expected.sort_index(1) + expected = expected.sort_index(axis=1) tm.assert_frame_equal(df, expected) def test_loc_setitem_uint_drop(self, any_int_dtype): diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index 4df6f52e0fff4..d7bd92c673e69 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -320,3 +320,15 @@ def test_sort_values_key_type(self): result = s.sort_index(key=lambda x: x.month_name()) expected = s.iloc[[2, 1, 0]] tm.assert_series_equal(result, expected) + + def test_sort_index_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + ser = Series([1, 2, 3]) + msg = ( + r"In a future version of pandas all arguments of Series.sort_index " + r"will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser.sort_index(0) + expected = Series([1, 2, 3]) + tm.assert_series_equal(result, expected)