Skip to content

Commit 6a5e56d

Browse files
Erotemicgfyoung
authored andcommitted
BUG: np.inf now causes Index to upcast from int to float (#16996)
Closes gh-16957.
1 parent 9e7666d commit 6a5e56d

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Bug Fixes
179179
~~~~~~~~~
180180

181181
- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
182+
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)
182183

183184
Conversion
184185
^^^^^^^^^^

pandas/core/indexes/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def _try_convert_to_int_index(cls, data, copy, name):
666666
res = data.astype('u8', copy=False)
667667
if (res == data).all():
668668
return UInt64Index(res, copy=copy, name=name)
669-
except (TypeError, ValueError):
669+
except (OverflowError, TypeError, ValueError):
670670
pass
671671

672672
raise ValueError
@@ -1640,7 +1640,7 @@ def __contains__(self, key):
16401640
hash(key)
16411641
try:
16421642
return key in self._engine
1643-
except (TypeError, ValueError):
1643+
except (OverflowError, TypeError, ValueError):
16441644
return False
16451645

16461646
_index_shared_docs['contains'] = """
@@ -3365,7 +3365,7 @@ def _maybe_cast_indexer(self, key):
33653365
ckey = int(key)
33663366
if ckey == key:
33673367
key = ckey
3368-
except (ValueError, TypeError):
3368+
except (OverflowError, ValueError, TypeError):
33693369
pass
33703370
return key
33713371

pandas/tests/indexing/test_indexing.py

+56
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ def f():
6363

6464
pytest.raises(ValueError, f)
6565

66+
def test_inf_upcast(self):
67+
# GH 16957
68+
# We should be able to use np.inf as a key
69+
# np.inf should cause an index to convert to float
70+
71+
# Test with np.inf in rows
72+
df = pd.DataFrame(columns=[0])
73+
df.loc[1] = 1
74+
df.loc[2] = 2
75+
df.loc[np.inf] = 3
76+
77+
# make sure we can look up the value
78+
assert df.loc[np.inf, 0] == 3
79+
80+
result = df.index
81+
expected = pd.Float64Index([1, 2, np.inf])
82+
tm.assert_index_equal(result, expected)
83+
84+
# Test with np.inf in columns
85+
df = pd.DataFrame()
86+
df.loc[0, 0] = 1
87+
df.loc[1, 1] = 2
88+
df.loc[0, np.inf] = 3
89+
90+
result = df.columns
91+
expected = pd.Float64Index([0, 1, np.inf])
92+
tm.assert_index_equal(result, expected)
93+
6694
def test_setitem_dtype_upcast(self):
6795

6896
# GH3216
@@ -542,6 +570,34 @@ def test_astype_assignment_with_dups(self):
542570
# result = df.get_dtype_counts().sort_index()
543571
# expected = Series({'float64': 2, 'object': 1}).sort_index()
544572

573+
@pytest.mark.parametrize("index,val", [
574+
(pd.Index([0, 1, 2]), 2),
575+
(pd.Index([0, 1, '2']), '2'),
576+
(pd.Index([0, 1, 2, np.inf, 4]), 4),
577+
(pd.Index([0, 1, 2, np.nan, 4]), 4),
578+
(pd.Index([0, 1, 2, np.inf]), np.inf),
579+
(pd.Index([0, 1, 2, np.nan]), np.nan),
580+
])
581+
def test_index_contains(self, index, val):
582+
assert val in index
583+
584+
@pytest.mark.parametrize("index,val", [
585+
(pd.Index([0, 1, 2]), '2'),
586+
(pd.Index([0, 1, '2']), 2),
587+
(pd.Index([0, 1, 2, np.inf]), 4),
588+
(pd.Index([0, 1, 2, np.nan]), 4),
589+
(pd.Index([0, 1, 2, np.inf]), np.nan),
590+
(pd.Index([0, 1, 2, np.nan]), np.inf),
591+
# Checking if np.inf in Int64Index should not cause an OverflowError
592+
# Related to GH 16957
593+
(pd.Int64Index([0, 1, 2]), np.inf),
594+
(pd.Int64Index([0, 1, 2]), np.nan),
595+
(pd.UInt64Index([0, 1, 2]), np.inf),
596+
(pd.UInt64Index([0, 1, 2]), np.nan),
597+
])
598+
def test_index_not_contains(self, index, val):
599+
assert val not in index
600+
545601
def test_index_type_coercion(self):
546602

547603
with catch_warnings(record=True):

0 commit comments

Comments
 (0)