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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Bug Fixes

**Indexing**

-
- Fix OverflowError when trying to use 'inf' as na_value with int index column
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number here, use double-backticks around OverflowError. use double backticks on na_value; spell out int -> integer.

-

**I/O**
Expand Down
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
10 changes: 10 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,13 @@ 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):
data = "idx,col1,col2\n1,3,4\n2,inf,-inf"
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number here as a comment


# Don't fail with OverflowError with infs and int 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)