Skip to content

Commit 562b4bd

Browse files
committed
Merge pull request #4220 from jtratner/expand-inf-comparisons
ENH: Treat 'Inf' as infinity in text parser
2 parents 6c8fca9 + 3080721 commit 562b4bd

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

doc/source/release.rst

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pandas 0.13
4141
- ``read_excel`` now supports an integer in its ``sheetname`` argument giving
4242
the index of the sheet to read in (:issue:`4301`).
4343
- Added a test for ``read_clipboard()`` and ``to_clipboard()`` (:issue:`4282`)
44+
- Text parser now treats anything that reads like inf ("inf", "Inf", "-Inf",
45+
"iNf", etc.) to infinity. (:issue:`4220`, :issue:`4219`), affecting
46+
``read_table``, ``read_csv``, etc.
4447

4548
**API Changes**
4649

doc/source/v0.13.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ API changes
1111

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

1518
Enhancements
1619
~~~~~~~~~~~~

pandas/io/tests/test_parsers.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,22 @@ def test_inf_parsing(self):
167167
data = """\
168168
,A
169169
a,inf
170-
b,-inf"""
170+
b,-inf
171+
c,Inf
172+
d,-Inf
173+
e,INF
174+
f,-INF
175+
g,INf
176+
h,-INf
177+
i,inF
178+
j,-inF"""
179+
inf = float('inf')
180+
expected = Series([inf, -inf] * 5)
171181
df = read_csv(StringIO(data), index_col=0)
172-
self.assertTrue(np.isinf(np.abs(df['A'])).all())
182+
assert_almost_equal(df['A'].values, expected.values)
183+
df = read_csv(StringIO(data), index_col=0, na_filter=False)
184+
print df['A'].values
185+
assert_almost_equal(df['A'].values, expected.values)
173186

174187
def test_multiple_date_col(self):
175188
# Can use multiple date parsers

pandas/parser.pyx

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from libc.stdio cimport fopen, fclose
55
from libc.stdlib cimport malloc, free
6-
from libc.string cimport strncpy, strlen, strcmp
6+
from libc.string cimport strncpy, strlen, strcmp, strcasecmp
77
cimport libc.stdio as stdio
88

99
from cpython cimport (PyObject, PyBytes_FromString,
@@ -1399,9 +1399,9 @@ cdef _try_double(parser_t *parser, int col, int line_start, int line_end,
13991399
else:
14001400
error = to_double(word, data, parser.sci, parser.decimal)
14011401
if error != 1:
1402-
if strcmp(word, cinf) == 0:
1402+
if strcasecmp(word, cinf) == 0:
14031403
data[0] = INF
1404-
elif strcmp(word, cneginf) == 0:
1404+
elif strcasecmp(word, cneginf) == 0:
14051405
data[0] = NEGINF
14061406
else:
14071407
return None, None
@@ -1415,9 +1415,9 @@ cdef _try_double(parser_t *parser, int col, int line_start, int line_end,
14151415
word = COLITER_NEXT(it)
14161416
error = to_double(word, data, parser.sci, parser.decimal)
14171417
if error != 1:
1418-
if strcmp(word, cinf) == 0:
1418+
if strcasecmp(word, cinf) == 0:
14191419
data[0] = INF
1420-
elif strcmp(word, cneginf) == 0:
1420+
elif strcasecmp(word, cneginf) == 0:
14211421
data[0] = NEGINF
14221422
else:
14231423
return None, None

pandas/util/testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def assert_almost_equal(a, b, check_less_precise = False):
126126
return assert_dict_equal(a, b)
127127

128128
if isinstance(a, basestring):
129-
assert a == b, "%s != %s" % (a, b)
129+
assert a == b, "%r != %r" % (a, b)
130130
return True
131131

132132
if isiterable(a):

0 commit comments

Comments
 (0)