Skip to content

BUG: read csv not breaking lines for warn messages #45677

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
Jan 28, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ I/O
^^^
- Bug in :meth:`DataFrame.to_stata` where no error is raised if the :class:`DataFrame` contains ``-np.inf`` (:issue:`45350`)
- Bug in :meth:`DataFrame.info` where a new line at the end of the output is omitted when called on an empty :class:`DataFrame` (:issue:`45494`)
- Bug in :func:`read_csv` not recognizing line break for ``on_bad_lines="warn"`` for ``engine="c"`` (:issue:`41710`)
- Bug in :func:`read_parquet` when ``engine="pyarrow"`` which caused partial write to disk when column of unsupported datatype was passed (:issue:`44914`)
-

Expand Down
9 changes: 7 additions & 2 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2012, Lambda Foundry, Inc.
# See LICENSE for the license
from base64 import decode
from csv import (
QUOTE_MINIMAL,
QUOTE_NONE,
Expand Down Expand Up @@ -839,7 +840,9 @@ cdef class TextReader:
status = tokenize_nrows(self.parser, nrows, self.encoding_errors)

if self.parser.warn_msg != NULL:
print(self.parser.warn_msg, file=sys.stderr)
print(PyUnicode_DecodeUTF8(
self.parser.warn_msg, strlen(self.parser.warn_msg),
self.encoding_errors), file=sys.stderr)
free(self.parser.warn_msg)
self.parser.warn_msg = NULL

Expand Down Expand Up @@ -868,7 +871,9 @@ cdef class TextReader:
status = tokenize_all_rows(self.parser, self.encoding_errors)

if self.parser.warn_msg != NULL:
print(self.parser.warn_msg, file=sys.stderr)
print(PyUnicode_DecodeUTF8(
self.parser.warn_msg, strlen(self.parser.warn_msg),
self.encoding_errors), file=sys.stderr)
free(self.parser.warn_msg)
self.parser.warn_msg = NULL

Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/io/parser/common/test_read_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,30 @@ def test_conflict_on_bad_line(all_parsers, error_bad_lines, warn_bad_lines):
"Please only set on_bad_lines.",
):
parser.read_csv(StringIO(data), on_bad_lines="error", **kwds)


def test_on_bad_lines_warn_correct_formatting(all_parsers, capsys):
# see gh-15925
parser = all_parsers
data = """1,2
a,b
a,b,c
a,b,d
a,b
"""
expected = DataFrame({"1": "a", "2": ["b"] * 2})

result = parser.read_csv(StringIO(data), on_bad_lines="warn")
tm.assert_frame_equal(result, expected)

captured = capsys.readouterr()
if parser.engine == "c":
warn = """Skipping line 3: expected 2 fields, saw 3
Skipping line 4: expected 2 fields, saw 3

"""
else:
warn = """Skipping line 3: Expected 2 fields in line 3, saw 3
Skipping line 4: Expected 2 fields in line 4, saw 3
"""
assert captured.err == warn