Skip to content

Commit 9f65984

Browse files
authored
Deprecate passing args as positional in sort_values (#41505)
1 parent fd38824 commit 9f65984

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

doc/source/whatsnew/v1.3.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ Deprecations
682682
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (other than ``"upper"`` and ``"lower"``) (:issue:`41485`)
683683
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
684684
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
685+
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_values` (other than ``"by"``) and :meth:`Series.sort_values` (:issue:`41485`)
685686
- Deprecated passing arguments as positional in :meth:`DataFrame.dropna` and :meth:`Series.dropna` (:issue:`41485`)
686687
- Deprecated passing arguments as positional in :meth:`DataFrame.set_index` (other than ``"keys"``) (:issue:`41485`)
687688
- Deprecated passing arguments as positional (except for ``"levels"``) in :meth:`MultiIndex.set_levels` (:issue:`41485`)
@@ -691,6 +692,7 @@ Deprecations
691692
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
692693
- 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`)
693694
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
695+
-
694696

695697
.. _whatsnew_130.deprecations.nuisance_columns:
696698

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -6221,6 +6221,7 @@ def f(vals) -> tuple[np.ndarray, int]:
62216221
# ----------------------------------------------------------------------
62226222
# Sorting
62236223
# TODO: Just move the sort_values doc here.
6224+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "by"])
62246225
@Substitution(**_shared_doc_kwargs)
62256226
@Appender(NDFrame.sort_values.__doc__)
62266227
# error: Signature of "sort_values" incompatible with supertype "NDFrame"

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,7 @@ def update(self, other) -> None:
32593259
# ----------------------------------------------------------------------
32603260
# Reindexing, sorting
32613261

3262+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
32623263
def sort_values(
32633264
self,
32643265
axis=0,

pandas/tests/frame/methods/test_sort_values.py

+12
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,15 @@ 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"In a future version of pandas all arguments of DataFrame\.sort_values "
865+
r"except for the argument 'by' will be keyword-only"
866+
)
867+
with tm.assert_produces_warning(FutureWarning, match=msg):
868+
result = df.sort_values("a", 0)
869+
expected = DataFrame({"a": [1, 2, 3]})
870+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_sort_values.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -187,30 +187,42 @@ 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"In a future version of pandas all arguments of Series\.sort_values "
195+
r"will be keyword-only"
196+
)
197+
with tm.assert_produces_warning(FutureWarning, match=msg):
198+
result = ser.sort_values(0)
199+
expected = Series([1, 2, 3])
200+
tm.assert_series_equal(result, expected)
201+
190202

191203
class TestSeriesSortingKey:
192204
def test_sort_values_key(self):
193205
series = Series(np.array(["Hello", "goodbye"]))
194206

195-
result = series.sort_values(0)
207+
result = series.sort_values(axis=0)
196208
expected = series
197209
tm.assert_series_equal(result, expected)
198210

199-
result = series.sort_values(0, key=lambda x: x.str.lower())
211+
result = series.sort_values(axis=0, key=lambda x: x.str.lower())
200212
expected = series[::-1]
201213
tm.assert_series_equal(result, expected)
202214

203215
def test_sort_values_key_nan(self):
204216
series = Series(np.array([0, 5, np.nan, 3, 2, np.nan]))
205217

206-
result = series.sort_values(0)
218+
result = series.sort_values(axis=0)
207219
expected = series.iloc[[0, 4, 3, 1, 2, 5]]
208220
tm.assert_series_equal(result, expected)
209221

210-
result = series.sort_values(0, key=lambda x: x + 5)
222+
result = series.sort_values(axis=0, key=lambda x: x + 5)
211223
expected = series.iloc[[0, 4, 3, 1, 2, 5]]
212224
tm.assert_series_equal(result, expected)
213225

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

0 commit comments

Comments
 (0)