From 0414e7dd29f4f6c03eb99e04d3589e6f2e27b2c2 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 10 Feb 2023 18:42:06 +0100 Subject: [PATCH] BUG: idxmax and idxmin raising for all na ea series --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/sorting.py | 2 ++ pandas/tests/series/methods/test_argsort.py | 7 +++++++ 3 files changed, 10 insertions(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index d1bc322a6ec7a..eb72bee265c1b 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1256,6 +1256,7 @@ Missing - Bug in :meth:`DataFrame.update` with ``overwrite=False`` raising ``TypeError`` when ``self`` has column with ``NaT`` values and column not present in ``other`` (:issue:`16713`) - Bug in :meth:`Series.replace` raising ``RecursionError`` when replacing value in object-dtype :class:`Series` containing ``NA`` (:issue:`47480`) - Bug in :meth:`Series.replace` raising ``RecursionError`` when replacing value in numeric :class:`Series` with ``NA`` (:issue:`50758`) +- Bug in :meth:`Series.idxmin` and :meth:`Series.idxmax` raising ``ValueError`` for all ``NA`` with extension array dtype (:issue:`51276`) MultiIndex ^^^^^^^^^^ diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index b28a9def8a7ea..807afb1ff9d61 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -461,6 +461,8 @@ def nargminmax(values: ExtensionArray, method: str, axis: AxisInt = 0): func = np.argmax if method == "argmax" else np.argmin mask = np.asarray(isna(values)) + if mask.all(): + return -1 arr_values = values._values_for_argsort() if arr_values.ndim > 1: diff --git a/pandas/tests/series/methods/test_argsort.py b/pandas/tests/series/methods/test_argsort.py index 1fbc9ed787e11..ba5800c5dd315 100644 --- a/pandas/tests/series/methods/test_argsort.py +++ b/pandas/tests/series/methods/test_argsort.py @@ -65,3 +65,10 @@ def test_argsort_stable(self): def test_argsort_preserve_name(self, datetime_series): result = datetime_series.argsort() assert result.name == datetime_series.name + + @pytest.mark.parametrize("func", ["idxmin", "idxmax"]) + def test_idxmin_idxmax_all_na(self, any_numeric_ea_and_arrow_dtype, func): + # GH#51276 + ser = Series([None, None], dtype=any_numeric_ea_and_arrow_dtype) + result = getattr(ser, func)() + assert result is np.nan