Skip to content

ENH: Enable read_csv interpret 'Infinity' as floating point value #10065 #28181

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 5 commits into from
Sep 2, 2019
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ I/O

- :meth:`read_csv` now accepts binary mode file buffers when using the Python csv engine (:issue:`23779`)
- Bug in :meth:`DataFrame.to_json` where using a Tuple as a column or index value and using ``orient="columns"`` or ``orient="index"`` would produce invalid JSON (:issue:`20500`)
-
- Improve infinity parsing. :meth:`read_csv` now interprets ``Infinity``, ``+Infinity``, ``-Infinity`` as floating point values (:issue:`10065`)

Plotting
^^^^^^^^
Expand Down
18 changes: 14 additions & 4 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,10 @@ cdef:
char* cposinf = b'+inf'
char* cneginf = b'-inf'

char* cinfty = b'Infinity'
char* cposinfty = b'+Infinity'
char* cneginfty = b'-Infinity'


cdef _try_double(parser_t *parser, int64_t col,
int64_t line_start, int64_t line_end,
Expand Down Expand Up @@ -1772,9 +1776,12 @@ cdef inline int _try_double_nogil(parser_t *parser,
if error != 0 or p_end == word or p_end[0]:
error = 0
if (strcasecmp(word, cinf) == 0 or
strcasecmp(word, cposinf) == 0):
strcasecmp(word, cposinf) == 0 or
strcasecmp(word, cinfty) == 0 or
strcasecmp(word, cposinfty) == 0):
data[0] = INF
elif strcasecmp(word, cneginf) == 0:
elif (strcasecmp(word, cneginf) == 0 or
strcasecmp(word, cneginfty) == 0 ):
data[0] = NEGINF
else:
return 1
Expand All @@ -1793,9 +1800,12 @@ cdef inline int _try_double_nogil(parser_t *parser,
if error != 0 or p_end == word or p_end[0]:
error = 0
if (strcasecmp(word, cinf) == 0 or
strcasecmp(word, cposinf) == 0):
strcasecmp(word, cposinf) == 0 or
strcasecmp(word, cinfty) == 0 or
strcasecmp(word, cposinfty) == 0):
data[0] = INF
elif strcasecmp(word, cneginf) == 0:
elif (strcasecmp(word, cneginf) == 0 or
strcasecmp(word, cneginfty) == 0):
data[0] = NEGINF
else:
return 1
Expand Down
19 changes: 18 additions & 1 deletion pandas/_libs/src/parse_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int floatify(PyObject *str, double *result, int *maybe_int) {
status = to_double(data, result, sci, dec, maybe_int);

if (!status) {
/* handle inf/-inf */
/* handle inf/-inf infinity/-infinity */
if (strlen(data) == 3) {
if (0 == strcasecmp(data, "inf")) {
*result = HUGE_VAL;
Expand All @@ -68,6 +68,23 @@ int floatify(PyObject *str, double *result, int *maybe_int) {
} else {
goto parsingerror;
}
} else if (strlen(data) == 8) {
if (0 == strcasecmp(data, "infinity")) {
*result = HUGE_VAL;
*maybe_int = 0;
} else {
goto parsingerror;
}
} else if (strlen(data) == 9) {
if (0 == strcasecmp(data, "-infinity")) {
*result = -HUGE_VAL;
*maybe_int = 0;
} else if (0 == strcasecmp(data, "+infinity")) {
*result = HUGE_VAL;
*maybe_int = 0;
} else {
goto parsingerror;
}
} else {
goto parsingerror;
}
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,23 @@ def test_inf_parsing(all_parsers, na_filter):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("na_filter", [True, False])
def test_infinity_parsing(all_parsers, na_filter):
parser = all_parsers
data = """\
,A
a,Infinity
b,-Infinity
c,+Infinity
"""
expected = DataFrame(
{"A": [float("infinity"), float("-infinity"), float("+infinity")]},
index=["a", "b", "c"],
)
result = parser.read_csv(StringIO(data), index_col=0, na_filter=na_filter)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("nrows", [0, 1, 2, 3, 4, 5])
def test_raise_on_no_columns(all_parsers, nrows):
parser = all_parsers
Expand Down