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 2 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
7 changes: 6 additions & 1 deletion crates/polars-io/src/csv/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,19 @@ 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();

let len = line.len();

if len == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if line.is_empty()

continue;
}

if let Some(c) = comment_char {
// line is a comment -> skip
if line[0] == c {
continue;
}
}

let len = line.len();
if len > 1 {
// remove carriage return
let trailing_byte = line[len - 1];
Expand Down
26 changes: 26 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,32 @@ 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"]})
print(df)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the print?

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"]})
print(df)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the print?

assert_frame_equal(df, expected)


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