From b49d40ace94ae5742b7946a5126b5ae822271a8e Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 25 Jul 2023 09:34:19 -0700 Subject: [PATCH 1/2] BUG: Series.argsort validate axis --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/series.py | 4 ++++ pandas/tests/series/methods/test_argsort.py | 21 ++++++++++++++------- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 44318df408751..11568929d9499 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -646,6 +646,7 @@ Other - 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`) - Bug in :meth:`period_range` the default behavior when freq was not passed as an argument was incorrect(:issue:`53687`) - Fixed incorrect ``__name__`` attribute of ``pandas._libs.json`` (:issue:`52898`) +- Bug in :meth:`Series.argsort` failing to raise when an invalid ``axis`` is passed (:issue:`??`) - .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/core/series.py b/pandas/core/series.py index 81eea98ab3f65..62fff5851586c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3958,6 +3958,10 @@ def argsort( 2 0 dtype: int64 """ + if axis != -1: + # We allow -1 here so that np.argsort(series) works + self._get_axis_number(axis) + values = self._values mask = isna(values) diff --git a/pandas/tests/series/methods/test_argsort.py b/pandas/tests/series/methods/test_argsort.py index 8549ecb99d061..500b2a94bd511 100644 --- a/pandas/tests/series/methods/test_argsort.py +++ b/pandas/tests/series/methods/test_argsort.py @@ -10,12 +10,19 @@ class TestSeriesArgsort: + def test_argsort_axis(self): + ser = Series(range(3)) + + msg = "No axis named 2 for object type Series" + with pytest.raises(ValueError, match=msg): + ser.argsort(axis=2) + def test_argsort_numpy(self, datetime_series): ser = datetime_series - func = np.argsort - tm.assert_numpy_array_equal( - func(ser).values, func(np.array(ser)), check_dtype=False - ) + + res = np.argsort(ser).values + expected = np.argsort(np.array(ser)) + tm.assert_numpy_array_equal(res, expected) # with missing values ts = ser.copy() @@ -25,10 +32,10 @@ def test_argsort_numpy(self, datetime_series): with tm.assert_produces_warning( FutureWarning, match=msg, check_stacklevel=False ): - result = func(ts)[1::2] - expected = func(np.array(ts.dropna())) + result = np.argsort(ts)[1::2] + expected = np.argsort(np.array(ts.dropna())) - tm.assert_numpy_array_equal(result.values, expected, check_dtype=False) + tm.assert_numpy_array_equal(result.values, expected) def test_argsort(self, datetime_series): argsorted = datetime_series.argsort() From 750fa73c03092b9946d62f655621468adee0e30b Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 25 Jul 2023 09:35:14 -0700 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/series.py | 2 +- pandas/tests/series/methods/test_argsort.py | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 11568929d9499..310b42e373dae 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -642,11 +642,11 @@ Other - Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`) - Bug in :meth:`Index.sort_values` when a ``key`` is passed (:issue:`52764`) - 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`) +- Bug in :meth:`Series.argsort` failing to raise when an invalid ``axis`` is passed (:issue:`54257`) - 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`) - 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`) - Bug in :meth:`period_range` the default behavior when freq was not passed as an argument was incorrect(:issue:`53687`) - Fixed incorrect ``__name__`` attribute of ``pandas._libs.json`` (:issue:`52898`) -- Bug in :meth:`Series.argsort` failing to raise when an invalid ``axis`` is passed (:issue:`??`) - .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/core/series.py b/pandas/core/series.py index 62fff5851586c..21cce4c491e14 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3959,7 +3959,7 @@ def argsort( dtype: int64 """ if axis != -1: - # We allow -1 here so that np.argsort(series) works + # GH#54257 We allow -1 here so that np.argsort(series) works self._get_axis_number(axis) values = self._values diff --git a/pandas/tests/series/methods/test_argsort.py b/pandas/tests/series/methods/test_argsort.py index 500b2a94bd511..bb2c29f9acdbb 100644 --- a/pandas/tests/series/methods/test_argsort.py +++ b/pandas/tests/series/methods/test_argsort.py @@ -11,6 +11,7 @@ class TestSeriesArgsort: def test_argsort_axis(self): + # GH#54257 ser = Series(range(3)) msg = "No axis named 2 for object type Series"