Skip to content

BUG: read_csv not recognizing bad lines with names given #44646

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 2 commits into from
Nov 28, 2021
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: 2 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ I/O
- Bug in :func:`read_json` not handling non-numpy dtypes correctly (especially ``category``) (:issue:`21892`, :issue:`33205`)
- Bug in :func:`json_normalize` where multi-character ``sep`` parameter is incorrectly prefixed to every key (:issue:`43831`)
- Bug in :func:`json_normalize` where reading data with missing multi-level metadata would not respect errors="ignore" (:issue:`44312`)
- Bug in :func:`read_csv` used second row to guess implicit index if ``header`` was set to ``None`` for ``engine="python"`` (:issue:`22144`)
- Bug in :func:`read_csv` not recognizing bad lines when ``names`` were given for ``engine="c"`` (:issue:`22144`)
- Bug in :func:`read_csv` with :code:`float_precision="round_trip"` which did not skip initial/trailing whitespace (:issue:`43713`)
- Bug in dumping/loading a :class:`DataFrame` with ``yaml.dump(frame)`` (:issue:`42748`)
- Bug in :class:`ExcelWriter`, where ``engine_kwargs`` were not passed through to all engines (:issue:`43442`)
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,6 @@ cdef class TextReader:
header = [self.names]

elif self.names is not None:
# Enforce this unless usecols
if not self.has_usecols:
self.parser.expected_fields = len(self.names)

# Names passed
if self.parser.lines < 1:
self._tokenize_rows(1)
Expand All @@ -735,6 +731,10 @@ cdef class TextReader:
field_count = len(header[0])
else:
field_count = self.parser.line_fields[data_line]

# Enforce this unless usecols
if not self.has_usecols:
self.parser.expected_fields = max(field_count, len(self.names))
else:
# No header passed nor to be found in the file
if self.parser.lines < 1:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ static int end_line(parser_t *self) {
}

if (!(self->lines <= self->header_end + 1) &&
(self->expected_fields < 0 && fields > ex_fields) && !(self->usecols)) {
(fields > ex_fields) && !(self->usecols)) {
// increment file line count
self->file_lines++;

Expand Down
14 changes: 10 additions & 4 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def _infer_columns(self):
num_original_columns = 0
clear_buffer = True
unnamed_cols: set[str | int | None] = set()
self._header_line = None

if self.header is not None:
header = self.header
Expand Down Expand Up @@ -496,6 +497,8 @@ def _infer_columns(self):

line = names[:]

# Store line, otherwise it is lost for guessing the index
self._header_line = line
ncols = len(line)
num_original_columns = ncols

Expand Down Expand Up @@ -867,10 +870,13 @@ def _get_index_name(self, columns):
orig_names = list(columns)
columns = list(columns)

try:
line = self._next_line()
except StopIteration:
line = None
if self._header_line is not None:
line = self._header_line
else:
try:
line = self._next_line()
except StopIteration:
line = None

try:
next_line = self._next_line()
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/io/parser/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,36 @@ def test_read_csv_multi_header_length_check(all_parsers):
ParserError, match="Header rows must have an equal number of columns."
):
parser.read_csv(StringIO(case), header=[0, 2])


@skip_pyarrow
def test_header_none_and_implicit_index(all_parsers):
# GH#22144
parser = all_parsers
data = "x,1,5\ny,2\nz,3\n"
result = parser.read_csv(StringIO(data), names=["a", "b"], header=None)
expected = DataFrame(
{"a": [1, 2, 3], "b": [5, np.nan, np.nan]}, index=["x", "y", "z"]
)
tm.assert_frame_equal(result, expected)


@skip_pyarrow
def test_header_none_and_implicit_index_in_second_row(all_parsers):
# GH#22144
parser = all_parsers
data = "x,1\ny,2,5\nz,3\n"
with pytest.raises(ParserError, match="Expected 2 fields in line 2, saw 3"):
parser.read_csv(StringIO(data), names=["a", "b"], header=None)


@skip_pyarrow
def test_header_none_and_on_bad_lines_skip(all_parsers):
# GH#22144
parser = all_parsers
data = "x,1\ny,2,5\nz,3\n"
result = parser.read_csv(
StringIO(data), names=["a", "b"], header=None, on_bad_lines="skip"
)
expected = DataFrame({"a": ["x", "z"], "b": [1, 3]})
tm.assert_frame_equal(result, expected)