Skip to content
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

fix: read_csv for empty lines #11924

Merged
merged 3 commits into from
Oct 22, 2023
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
4 changes: 4 additions & 0 deletions crates/polars-io/src/csv/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ pub fn infer_file_schema_inner(
// keep track so that we can determine the amount of bytes read
end_ptr = line.as_ptr() as usize + line.len();

if line.is_empty() {
continue;
}

if let Some(c) = comment_char {
// line is a comment -> skip
if line[0] == c {
Expand Down
24 changes: 24 additions & 0 deletions py-polars/tests/unit/io/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,30 @@ def test_empty_bytes() -> None:
assert_frame_equal(df, pl.DataFrame())


def test_empty_line_with_single_column() -> None:
df = pl.read_csv(
b"a\n\nb\n",
new_columns=["A"],
has_header=False,
comment_char="#",
use_pyarrow=False,
)
expected = pl.DataFrame({"A": ["a", None, "b"]})
assert_frame_equal(df, expected)


def test_empty_line_with_multiple_columns() -> None:
df = pl.read_csv(
b"a,b\n\nc,d\n",
new_columns=["A", "B"],
has_header=False,
comment_char="#",
use_pyarrow=False,
)
expected = pl.DataFrame({"A": ["a", "c"], "B": ["b", "d"]})
assert_frame_equal(df, expected)


def test_csv_quote_char() -> None:
expected = pl.DataFrame(
[
Expand Down