Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Handle infinite values correctly in Series.argmax #13625

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
6 changes: 2 additions & 4 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add a test that is the same as the original issue

def check_funs_ddof(self,
testfunc,
targfunc,
Expand All @@ -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,
Expand Down