Skip to content

Commit bef88ef

Browse files
natmokvalmroeschke
andauthored
CLN: enforce the deprecation of the Series.argsort NA behavior (#58232)
* enforce deprecation of the Series.argsort NA behavior * remove comments * add a note to v3.0.0 * correct def argsort and tests * correct def argsort/tests * fix pre-commit error * Restore numpy test --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 2d6e61e commit bef88ef

File tree

4 files changed

+11
-39
lines changed

4 files changed

+11
-39
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ Other Removals
382382
- Enforced deprecation of strings ``T``, ``L``, ``U``, and ``N`` denoting units in :class:`Timedelta` (:issue:`57627`)
383383
- Enforced deprecation of the behavior of :func:`concat` when ``len(keys) != len(objs)`` would truncate to the shorter of the two. Now this raises a ``ValueError`` (:issue:`43485`)
384384
- Enforced deprecation of the behavior of :meth:`DataFrame.replace` and :meth:`Series.replace` with :class:`CategoricalDtype` that would introduce new categories. (:issue:`58270`)
385+
- Enforced deprecation of the behavior of :meth:`Series.argsort` in the presence of NA values (:issue:`58232`)
385386
- Enforced deprecation of values "pad", "ffill", "bfill", and "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` (:issue:`57869`)
386387
- Enforced deprecation removing :meth:`Categorical.to_list`, use ``obj.tolist()`` instead (:issue:`51254`)
387388
- Enforced silent-downcasting deprecation for :ref:`all relevant methods <whatsnew_220.silent_downcasting>` (:issue:`54710`)

pandas/core/series.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
deprecate_nonkeyword_arguments,
5050
doc,
5151
)
52-
from pandas.util._exceptions import find_stack_level
5352
from pandas.util._validators import (
5453
validate_ascending,
5554
validate_bool_kwarg,
@@ -3722,25 +3721,7 @@ def argsort(
37223721
# GH#54257 We allow -1 here so that np.argsort(series) works
37233722
self._get_axis_number(axis)
37243723

3725-
values = self._values
3726-
mask = isna(values)
3727-
3728-
if mask.any():
3729-
# TODO(3.0): once this deprecation is enforced we can call
3730-
# self.array.argsort directly, which will close GH#43840 and
3731-
# GH#12694
3732-
warnings.warn(
3733-
"The behavior of Series.argsort in the presence of NA values is "
3734-
"deprecated. In a future version, NA values will be ordered "
3735-
"last instead of set to -1.",
3736-
FutureWarning,
3737-
stacklevel=find_stack_level(),
3738-
)
3739-
result = np.full(len(self), -1, dtype=np.intp)
3740-
notmask = ~mask
3741-
result[notmask] = np.argsort(values[notmask], kind=kind)
3742-
else:
3743-
result = np.argsort(values, kind=kind)
3724+
result = self.array.argsort(kind=kind)
37443725

37453726
res = self._constructor(
37463727
result, index=self.index, name=self.name, dtype=np.intp, copy=False

pandas/tests/extension/base/methods.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ def test_argsort_missing_array(self, data_missing_for_sorting):
116116
tm.assert_numpy_array_equal(result, expected)
117117

118118
def test_argsort_missing(self, data_missing_for_sorting):
119-
msg = "The behavior of Series.argsort in the presence of NA values"
120-
with tm.assert_produces_warning(FutureWarning, match=msg):
121-
result = pd.Series(data_missing_for_sorting).argsort()
122-
expected = pd.Series(np.array([1, -1, 0], dtype=np.intp))
119+
result = pd.Series(data_missing_for_sorting).argsort()
120+
expected = pd.Series(np.array([2, 0, 1], dtype=np.intp))
123121
tm.assert_series_equal(result, expected)
124122

125123
def test_argmin_argmax(self, data_for_sorting, data_missing_for_sorting, na_value):

pandas/tests/series/methods/test_argsort.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,15 @@ def test_argsort_axis(self):
2020

2121
def test_argsort_numpy(self, datetime_series):
2222
ser = datetime_series
23-
2423
res = np.argsort(ser).values
2524
expected = np.argsort(np.array(ser))
2625
tm.assert_numpy_array_equal(res, expected)
2726

28-
# with missing values
29-
ts = ser.copy()
30-
ts[::2] = np.nan
31-
32-
msg = "The behavior of Series.argsort in the presence of NA values"
33-
with tm.assert_produces_warning(
34-
FutureWarning, match=msg, check_stacklevel=False
35-
):
36-
result = np.argsort(ts)[1::2]
37-
expected = np.argsort(np.array(ts.dropna()))
27+
def test_argsort_numpy_missing(self):
28+
data = [0.1, np.nan, 0.2, np.nan, 0.3]
29+
ser = Series(data)
30+
result = np.argsort(ser)
31+
expected = np.argsort(np.array(data))
3832

3933
tm.assert_numpy_array_equal(result.values, expected)
4034

@@ -56,10 +50,8 @@ def test_argsort_dt64(self, unit):
5650
expected = Series(range(5), dtype=np.intp)
5751
tm.assert_series_equal(result, expected)
5852

59-
msg = "The behavior of Series.argsort in the presence of NA values"
60-
with tm.assert_produces_warning(FutureWarning, match=msg):
61-
result = shifted.argsort()
62-
expected = Series(list(range(4)) + [-1], dtype=np.intp)
53+
result = shifted.argsort()
54+
expected = Series(list(range(4)) + [4], dtype=np.intp)
6355
tm.assert_series_equal(result, expected)
6456

6557
def test_argsort_stable(self):

0 commit comments

Comments
 (0)