Skip to content

Commit d5f14dc

Browse files
jamie-harnessjbrockmendelmroeschke
authored
DEPR: Series.argsort NA behavior (pandas-dev#54219) (#26)
* DEPR: argsort * Catch warning in np.argsort test * Update doc/source/whatsnew/v2.1.0.rst * sort hwatnsew --------- Co-authored-by: jbrockmendel <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 51a862e commit d5f14dc

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ Deprecations
319319
- Deprecated the ``axis`` keyword in :meth:`DataFrame.ewm`, :meth:`Series.ewm`, :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.expanding`, :meth:`Series.expanding` (:issue:`51778`)
320320
- Deprecated the ``axis`` keyword in :meth:`DataFrame.resample`, :meth:`Series.resample` (:issue:`51778`)
321321
- Deprecated the behavior of :func:`concat` with both ``len(keys) != len(objs)``, in a future version this will raise instead of truncating to the shorter of the two sequences (:issue:`43485`)
322+
- Deprecated the behavior of :meth:`Series.argsort` in the presence of NA values; in a future version these will be sorted at the end instead of giving -1 (:issue:`54219`)
322323
- Deprecated the default of ``observed=False`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby`; this will default to ``True`` in a future version (:issue:`43999`)
323324
- Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`)
324325
- Deprecated the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`)

pandas/core/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -3962,6 +3962,13 @@ def argsort(
39623962
mask = isna(values)
39633963

39643964
if mask.any():
3965+
warnings.warn(
3966+
"The behavior of Series.argsort in the presence of NA values is "
3967+
"deprecated. In a future version, NA values will be ordered "
3968+
"last instead of set to -1.",
3969+
FutureWarning,
3970+
stacklevel=find_stack_level(),
3971+
)
39653972
result = np.full(len(self), -1, dtype=np.intp)
39663973
notmask = ~mask
39673974
result[notmask] = np.argsort(values[notmask], kind=kind)

pandas/tests/extension/base/methods.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ def test_argsort_missing_array(self, data_missing_for_sorting):
107107
tm.assert_numpy_array_equal(result, expected)
108108

109109
def test_argsort_missing(self, data_missing_for_sorting):
110-
result = pd.Series(data_missing_for_sorting).argsort()
110+
msg = "The behavior of Series.argsort in the presence of NA values"
111+
with tm.assert_produces_warning(FutureWarning, match=msg):
112+
result = pd.Series(data_missing_for_sorting).argsort()
111113
expected = pd.Series(np.array([1, -1, 0], dtype=np.intp))
112114
self.assert_series_equal(result, expected)
113115

pandas/tests/series/methods/test_argsort.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def test_argsort_numpy(self, datetime_series):
2121
ts = ser.copy()
2222
ts[::2] = np.NaN
2323

24-
result = func(ts)[1::2]
24+
msg = "The behavior of Series.argsort in the presence of NA values"
25+
with tm.assert_produces_warning(
26+
FutureWarning, match=msg, check_stacklevel=False
27+
):
28+
result = func(ts)[1::2]
2529
expected = func(np.array(ts.dropna()))
2630

2731
tm.assert_numpy_array_equal(result.values, expected, check_dtype=False)
@@ -41,7 +45,9 @@ def test_argsort(self, datetime_series):
4145
expected = Series(range(5), dtype=np.intp)
4246
tm.assert_series_equal(result, expected)
4347

44-
result = shifted.argsort()
48+
msg = "The behavior of Series.argsort in the presence of NA values"
49+
with tm.assert_produces_warning(FutureWarning, match=msg):
50+
result = shifted.argsort()
4551
expected = Series(list(range(4)) + [-1], dtype=np.intp)
4652
tm.assert_series_equal(result, expected)
4753

0 commit comments

Comments
 (0)