Skip to content

ENH: Treat 'Inf' as infinity in text parser #4220

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 1 commit into from
Jul 26, 2013
Merged
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
3 changes: 3 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pandas 0.13
- ``read_excel`` now supports an integer in its ``sheetname`` argument giving
the index of the sheet to read in (:issue:`4301`).
- Added a test for ``read_clipboard()`` and ``to_clipboard()`` (:issue:`4282`)
- Text parser now treats anything that reads like inf ("inf", "Inf", "-Inf",
"iNf", etc.) to infinity. (:issue:`4220`, :issue:`4219`), affecting
``read_table``, ``read_csv``, etc.

**API Changes**

Expand Down
3 changes: 3 additions & 0 deletions doc/source/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ API changes

- ``read_excel`` now supports an integer in its ``sheetname`` argument giving
the index of the sheet to read in (:issue:`4301`).
- Text parser now treats anything that reads like inf ("inf", "Inf", "-Inf",
"iNf", etc.) to infinity. (:issue:`4220`, :issue:`4219`), affecting
``read_table``, ``read_csv``, etc.

Enhancements
~~~~~~~~~~~~
Expand Down
17 changes: 15 additions & 2 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,22 @@ def test_inf_parsing(self):
data = """\
,A
a,inf
b,-inf"""
b,-inf
c,Inf
d,-Inf
e,INF
f,-INF
g,INf
h,-INf
i,inF
j,-inF"""
inf = float('inf')
expected = Series([inf, -inf] * 5)
df = read_csv(StringIO(data), index_col=0)
self.assertTrue(np.isinf(np.abs(df['A'])).all())
assert_almost_equal(df['A'].values, expected.values)
df = read_csv(StringIO(data), index_col=0, na_filter=False)
print df['A'].values
assert_almost_equal(df['A'].values, expected.values)

def test_multiple_date_col(self):
# Can use multiple date parsers
Expand Down
10 changes: 5 additions & 5 deletions pandas/parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from libc.stdio cimport fopen, fclose
from libc.stdlib cimport malloc, free
from libc.string cimport strncpy, strlen, strcmp
from libc.string cimport strncpy, strlen, strcmp, strcasecmp
cimport libc.stdio as stdio

from cpython cimport (PyObject, PyBytes_FromString,
Expand Down Expand Up @@ -1399,9 +1399,9 @@ cdef _try_double(parser_t *parser, int col, int line_start, int line_end,
else:
error = to_double(word, data, parser.sci, parser.decimal)
if error != 1:
if strcmp(word, cinf) == 0:
if strcasecmp(word, cinf) == 0:
data[0] = INF
elif strcmp(word, cneginf) == 0:
elif strcasecmp(word, cneginf) == 0:
data[0] = NEGINF
else:
return None, None
Expand All @@ -1415,9 +1415,9 @@ cdef _try_double(parser_t *parser, int col, int line_start, int line_end,
word = COLITER_NEXT(it)
error = to_double(word, data, parser.sci, parser.decimal)
if error != 1:
if strcmp(word, cinf) == 0:
if strcasecmp(word, cinf) == 0:
data[0] = INF
elif strcmp(word, cneginf) == 0:
elif strcasecmp(word, cneginf) == 0:
data[0] = NEGINF
else:
return None, None
Expand Down
2 changes: 1 addition & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def assert_almost_equal(a, b, check_less_precise = False):
return assert_dict_equal(a, b)

if isinstance(a, basestring):
assert a == b, "%s != %s" % (a, b)
assert a == b, "%r != %r" % (a, b)
return True

if isiterable(a):
Expand Down