Skip to content

BUG: Fix using "inf"/"-inf" in na_values for csv with int index column #22169

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

Merged
merged 14 commits into from
Aug 9, 2018
Merged
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.23.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ Bug Fixes

-
-

Copy link
Contributor

Choose a reason for hiding this comment

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

can you put this in the IO section, and referencde :func:read_csv here

**I/O**

- 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`)
4 changes: 2 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _ensure_data(values, dtype=None):
values = ensure_float64(values)
return values, 'float64', 'float64'

except (TypeError, ValueError):
except (TypeError, ValueError, OverflowError):
# if we are trying to coerce to a dtype
# and it is incompat this will fall thru to here
return ensure_object(values), 'object', 'object'
Expand Down Expand Up @@ -429,7 +429,7 @@ def isin(comps, values):
values = values.astype('int64', copy=False)
comps = comps.astype('int64', copy=False)
f = lambda x, y: htable.ismember_int64(x, y)
except (TypeError, ValueError):
except (TypeError, ValueError, OverflowError):
values = values.astype(object)
comps = comps.astype(object)

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/parser/na_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,14 @@ def test_no_na_filter_on_index(self):
expected = DataFrame({"a": [1, 4], "c": [3, 6]},
index=Index([np.nan, 5.0], name="b"))
tm.assert_frame_equal(out, expected)

def test_inf_na_values_with_int_index(self):
# see gh-17128
data = "idx,col1,col2\n1,3,4\n2,inf,-inf"

# Don't fail with OverflowError with infs and integer index column
out = self.read_csv(StringIO(data), index_col=[0],
na_values=['inf', '-inf'])
expected = DataFrame({"col1": [3, np.nan], "col2": [4, np.nan]},
index=Index([1, 2], name="idx"))
tm.assert_frame_equal(out, expected)