Skip to content

Commit d8da296

Browse files
committed
deprecate passing args as positional in sort_values
1 parent b2a36bd commit d8da296

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
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 (except for ``by``) in :meth:`DataFrame.sort_values` and :meth:`Series.sort_values` (:issue:`41485`)
650651

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

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -6188,6 +6188,7 @@ def f(vals) -> tuple[np.ndarray, int]:
61886188
# ----------------------------------------------------------------------
61896189
# Sorting
61906190
# TODO: Just move the sort_values doc here.
6191+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "by"])
61916192
@Substitution(**_shared_doc_kwargs)
61926193
@Appender(NDFrame.sort_values.__doc__)
61936194
# error: Signature of "sort_values" incompatible with supertype "NDFrame"

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -3224,6 +3224,7 @@ def update(self, other) -> None:
32243224
# ----------------------------------------------------------------------
32253225
# Reindexing, sorting
32263226

3227+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"])
32273228
def sort_values(
32283229
self,
32293230
axis=0,

pandas/tests/frame/methods/test_sort_values.py

+10
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,13 @@ def test_sort_column_level_and_index_label(
856856
tm.assert_frame_equal(result, expected)
857857
else:
858858
tm.assert_frame_equal(result, expected)
859+
860+
def test_sort_values_pos_args_deprecation(self):
861+
# https://github.com/pandas-dev/pandas/issues/41485
862+
df = DataFrame({"a": [1, 2, 3]})
863+
msg = (
864+
r"Starting with Pandas version 2\.0 all arguments of sort_values except "
865+
r"for the arguments 'self' and 'by' will be keyword-only"
866+
)
867+
with tm.assert_produces_warning(FutureWarning, match=msg):
868+
df.sort_values("a", 0)

pandas/tests/series/methods/test_sort_values.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -187,30 +187,40 @@ def test_sort_values_ignore_index(
187187
tm.assert_series_equal(result_ser, expected)
188188
tm.assert_series_equal(ser, Series(original_list))
189189

190+
def test_sort_values_pos_args_deprecation(self):
191+
# https://github.com/pandas-dev/pandas/issues/41485
192+
ser = Series([1, 2, 3])
193+
msg = (
194+
r"Starting with Pandas version 2\.0 all arguments of sort_values except "
195+
r"for the argument 'self' will be keyword-only"
196+
)
197+
with tm.assert_produces_warning(FutureWarning, match=msg):
198+
ser.sort_values(0)
199+
190200

191201
class TestSeriesSortingKey:
192202
def test_sort_values_key(self):
193203
series = Series(np.array(["Hello", "goodbye"]))
194204

195-
result = series.sort_values(0)
205+
result = series.sort_values(axis=0)
196206
expected = series
197207
tm.assert_series_equal(result, expected)
198208

199-
result = series.sort_values(0, key=lambda x: x.str.lower())
209+
result = series.sort_values(axis=0, key=lambda x: x.str.lower())
200210
expected = series[::-1]
201211
tm.assert_series_equal(result, expected)
202212

203213
def test_sort_values_key_nan(self):
204214
series = Series(np.array([0, 5, np.nan, 3, 2, np.nan]))
205215

206-
result = series.sort_values(0)
216+
result = series.sort_values(axis=0)
207217
expected = series.iloc[[0, 4, 3, 1, 2, 5]]
208218
tm.assert_series_equal(result, expected)
209219

210-
result = series.sort_values(0, key=lambda x: x + 5)
220+
result = series.sort_values(axis=0, key=lambda x: x + 5)
211221
expected = series.iloc[[0, 4, 3, 1, 2, 5]]
212222
tm.assert_series_equal(result, expected)
213223

214-
result = series.sort_values(0, key=lambda x: -x, ascending=False)
224+
result = series.sort_values(axis=0, key=lambda x: -x, ascending=False)
215225
expected = series.iloc[[0, 4, 3, 1, 2, 5]]
216226
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)