Skip to content

Commit faa1992

Browse files
meeseeksmachinejreback
authored andcommitted
Backport PR #22169: BUG: Fix using "inf"/"-inf" in na_values for csv with int index column (#22259)
1 parent c420e75 commit faa1992

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.23.5.txt

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ Bug Fixes
3737

3838
-
3939
-
40+
41+
**I/O**
42+
43+
- Bug in :func:`read_csv` that caused it to raise ``OverflowError`` when trying to use 'inf' as ``na_value`` with integer index column (:issue:`17128`)

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _ensure_data(values, dtype=None):
9595
values = _ensure_float64(values)
9696
return values, 'float64', 'float64'
9797

98-
except (TypeError, ValueError):
98+
except (TypeError, ValueError, OverflowError):
9999
# if we are trying to coerce to a dtype
100100
# and it is incompat this will fall thru to here
101101
return _ensure_object(values), 'object', 'object'
@@ -429,7 +429,7 @@ def isin(comps, values):
429429
values = values.astype('int64', copy=False)
430430
comps = comps.astype('int64', copy=False)
431431
f = lambda x, y: htable.ismember_int64(x, y)
432-
except (TypeError, ValueError):
432+
except (TypeError, ValueError, OverflowError):
433433
values = values.astype(object)
434434
comps = comps.astype(object)
435435

pandas/tests/io/parser/na_values.py

+11
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,14 @@ def test_no_na_filter_on_index(self):
369369
expected = DataFrame({"a": [1, 4], "c": [3, 6]},
370370
index=Index([np.nan, 5.0], name="b"))
371371
tm.assert_frame_equal(out, expected)
372+
373+
def test_inf_na_values_with_int_index(self):
374+
# see gh-17128
375+
data = "idx,col1,col2\n1,3,4\n2,inf,-inf"
376+
377+
# Don't fail with OverflowError with infs and integer index column
378+
out = self.read_csv(StringIO(data), index_col=[0],
379+
na_values=['inf', '-inf'])
380+
expected = DataFrame({"col1": [3, np.nan], "col2": [4, np.nan]},
381+
index=Index([1, 2], name="idx"))
382+
tm.assert_frame_equal(out, expected)

0 commit comments

Comments
 (0)