Skip to content

Commit

Permalink
Deprecate passing args as positional in sort_index (#41506)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed May 26, 2021
1 parent f373bba commit 0fd4aa7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/series/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0fd4aa7

Please sign in to comment.