Skip to content

Fix correct warning with c engine when skipping lines #16455

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
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/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Indexing
I/O
^^^

- Bug in pd.read_csv() when comment is passed in space deliminted text files (:issue:`16472`)
- Bug that would force importing of the clipboard routines unnecessarily, potentially causing an import error on startup (:issue:`16288`)
- Bug that raised IndexError HTML-rendering an empty DataFrame (:issue:`15953`)

Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,9 @@ int tokenize_bytes(parser_t *self, size_t line_limit, int start_lines) {
} else if (IS_CARRIAGE(c)) {
self->state = EAT_CRNL;
break;
} else if (IS_COMMENT_CHAR(c)) {
self->state = EAT_COMMENT;
break;
} else if (!IS_WHITESPACE(c)) {
self->state = START_FIELD;
// fall through to subsequent state
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/io/parser/c_parser_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
further arguments when parsing.
"""

import sys

import pytest
import numpy as np

Expand Down Expand Up @@ -417,3 +419,30 @@ def test_data_after_quote(self):
expected = DataFrame({'a': ['1', 'ba']})

tm.assert_frame_equal(result, expected)

@tm.capture_stderr
def test_comment_whitespace_delimited(self):
test_input = """\
1 2
2 2 3
3 2 3 # 3 fields
4 2 3# 3 fields
5 2 # 2 fields
6 2# 2 fields
7 # 1 field, NaN
8# 1 field, NaN
9 2 3 # skipped line
# comment"""
df = self.read_csv(StringIO(test_input), comment='#', header=None,
delimiter='\\s+', skiprows=0,
error_bad_lines=False)
error = sys.stderr.getvalue()
# skipped lines 2, 3, 4, 9
for line_num in (2, 3, 4, 9):
assert 'Skipping line {}'.format(line_num) in error, error
expected = DataFrame([[1, 2],
[5, 2],
[6, 2],
[7, np.nan],
[8, np.nan]])
tm.assert_frame_equal(df, expected)