diff --git a/doc/source/whatsnew/v0.19.0.txt b/doc/source/whatsnew/v0.19.0.txt index 4cc16aac15f8b..022d8545ca658 100644 --- a/doc/source/whatsnew/v0.19.0.txt +++ b/doc/source/whatsnew/v0.19.0.txt @@ -549,3 +549,5 @@ Bug Fixes - Bug in ``groupby`` with ``as_index=False`` returns all NaN's when grouping on multiple columns including a categorical one (:issue:`13204`) - Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`) + +- Bug when calling ``Series.argmax`` with infinite values (:issue:`13595`) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index f390e3f04a6c3..7c13c23f731da 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -454,8 +454,7 @@ def nanargmax(values, axis=None, skipna=True): """ Returns -1 in the NA case """ - values, mask, dtype, _ = _get_values(values, skipna, fill_value_typ='-inf', - isfinite=True) + values, mask, dtype, _ = _get_values(values, skipna, fill_value_typ='-inf') result = values.argmax(axis) result = _maybe_arg_null_out(result, axis, mask, skipna) return result @@ -465,8 +464,7 @@ def nanargmin(values, axis=None, skipna=True): """ Returns -1 in the NA case """ - values, mask, dtype, _ = _get_values(values, skipna, fill_value_typ='+inf', - isfinite=True) + values, mask, dtype, _ = _get_values(values, skipna, fill_value_typ='+inf') result = values.argmin(axis) result = _maybe_arg_null_out(result, axis, mask, skipna) return result diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index d9e2d8096c8d7..03c34b90c7a6c 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -1282,6 +1282,14 @@ def test_idxmax(self): result = s.idxmin() self.assertEqual(result, 1.1) + # Infinite values + # GH 13595 + s = pd.Series([1, 2, np.inf]) + result = s.idxmax() + self.assertEqual(result, 2) + result = s.idxmax(skipna=False) + self.assertEqual(result, 2) + def test_numpy_argmax(self): # argmax is aliased to idxmax diff --git a/pandas/tests/test_nanops.py b/pandas/tests/test_nanops.py index 904bedde03312..0c85ec9638ca9 100644 --- a/pandas/tests/test_nanops.py +++ b/pandas/tests/test_nanops.py @@ -41,6 +41,7 @@ def setUp(self): self.arr_inf = self.arr_float * np.inf self.arr_float_inf = np.vstack([self.arr_float, self.arr_inf]) + self.arr_float_neg_inf = -1 * self.arr_float_inf self.arr_float1_inf = np.vstack([self.arr_float1, self.arr_inf]) self.arr_inf_float1 = np.vstack([self.arr_inf, self.arr_float1]) self.arr_inf_inf = np.vstack([self.arr_inf, self.arr_inf]) @@ -234,7 +235,7 @@ def check_fun(self, testfunc, targfunc, testar, targar=None, def check_funs(self, testfunc, targfunc, allow_complex=True, allow_all_nan=True, allow_str=True, allow_date=True, - allow_tdelta=True, allow_obj=True, **kwargs): + allow_tdelta=True, allow_obj=True, allow_inf=True, **kwargs): self.check_fun(testfunc, targfunc, 'arr_float', **kwargs) self.check_fun(testfunc, targfunc, 'arr_float_nan', 'arr_float', **kwargs) @@ -287,6 +288,10 @@ def check_funs(self, testfunc, targfunc, allow_complex=True, allow_complex=allow_complex) self.check_fun(testfunc, targfunc, 'arr_obj', **kwargs) + if allow_inf: + self.check_fun(testfunc, targfunc, 'arr_float_inf', **kwargs) + self.check_fun(testfunc, targfunc, 'arr_float_neg_inf', **kwargs) + def check_funs_ddof(self, testfunc, targfunc, @@ -295,7 +300,7 @@ def check_funs_ddof(self, allow_str=True, allow_date=False, allow_tdelta=False, - allow_obj=True, ): + allow_obj=True): for ddof in range(3): try: self.check_funs(testfunc, targfunc, allow_complex,