Skip to content

Commit 4107aba

Browse files
authored
BUG: Series.argsort validate axis (#54257)
* BUG: Series.argsort validate axis * GH ref
1 parent 474c1e2 commit 4107aba

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ Other
643643
- Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`)
644644
- Bug in :meth:`Index.sort_values` when a ``key`` is passed (:issue:`52764`)
645645
- Bug in :meth:`Series.align`, :meth:`DataFrame.align`, :meth:`Series.reindex`, :meth:`DataFrame.reindex`, :meth:`Series.interpolate`, :meth:`DataFrame.interpolate`, incorrectly failing to raise with method="asfreq" (:issue:`53620`)
646+
- Bug in :meth:`Series.argsort` failing to raise when an invalid ``axis`` is passed (:issue:`54257`)
646647
- Bug in :meth:`Series.map` when giving a callable to an empty series, the returned series had ``object`` dtype. It now keeps the original dtype (:issue:`52384`)
647648
- Bug in :meth:`Series.memory_usage` when ``deep=True`` throw an error with Series of objects and the returned value is incorrect, as it does not take into account GC corrections (:issue:`51858`)
648649
- Bug in :meth:`period_range` the default behavior when freq was not passed as an argument was incorrect(:issue:`53687`)

pandas/core/series.py

+4
Original file line numberDiff line numberDiff line change
@@ -3958,6 +3958,10 @@ def argsort(
39583958
2 0
39593959
dtype: int64
39603960
"""
3961+
if axis != -1:
3962+
# GH#54257 We allow -1 here so that np.argsort(series) works
3963+
self._get_axis_number(axis)
3964+
39613965
values = self._values
39623966
mask = isna(values)
39633967

pandas/tests/series/methods/test_argsort.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@
1010

1111

1212
class TestSeriesArgsort:
13+
def test_argsort_axis(self):
14+
# GH#54257
15+
ser = Series(range(3))
16+
17+
msg = "No axis named 2 for object type Series"
18+
with pytest.raises(ValueError, match=msg):
19+
ser.argsort(axis=2)
20+
1321
def test_argsort_numpy(self, datetime_series):
1422
ser = datetime_series
15-
func = np.argsort
16-
tm.assert_numpy_array_equal(
17-
func(ser).values, func(np.array(ser)), check_dtype=False
18-
)
23+
24+
res = np.argsort(ser).values
25+
expected = np.argsort(np.array(ser))
26+
tm.assert_numpy_array_equal(res, expected)
1927

2028
# with missing values
2129
ts = ser.copy()
@@ -25,10 +33,10 @@ def test_argsort_numpy(self, datetime_series):
2533
with tm.assert_produces_warning(
2634
FutureWarning, match=msg, check_stacklevel=False
2735
):
28-
result = func(ts)[1::2]
29-
expected = func(np.array(ts.dropna()))
36+
result = np.argsort(ts)[1::2]
37+
expected = np.argsort(np.array(ts.dropna()))
3038

31-
tm.assert_numpy_array_equal(result.values, expected, check_dtype=False)
39+
tm.assert_numpy_array_equal(result.values, expected)
3240

3341
def test_argsort(self, datetime_series):
3442
argsorted = datetime_series.argsort()

0 commit comments

Comments
 (0)