Skip to content

Commit

Permalink
fix(scan_csv): handle empty csv file exception (#2934)
Browse files Browse the repository at this point in the history
  • Loading branch information
beowolx committed Mar 20, 2022
1 parent df27f82 commit 3c0b1d1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion polars/polars-lazy/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ impl LogicalPlanBuilder {
let path = path.into();
let mut file = std::fs::File::open(&path)?;
let mut magic_nr = [0u8; 2];
file.read_exact(&mut magic_nr)?;
file.read_exact(&mut magic_nr)
.map_err(|_| PolarsError::NoData("empty csv".into()))?;

if is_compressed(&magic_nr) {
return Err(PolarsError::ComputeError(
"cannot scan compressed csv; use read_csv for compressed data".into(),
Expand Down
Empty file added py-polars/tests/files/empty.csv
Empty file.
8 changes: 8 additions & 0 deletions py-polars/tests/io/test_lazy_csv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# flake8: noqa: W191,E101
import io
from os import path
from pathlib import Path

import numpy as np
import pytest

import polars as pl

Expand All @@ -12,6 +14,12 @@ def test_scan_csv() -> None:
assert df.collect().shape == (4, 3)


def test_scan_empty_csv() -> None:
with pytest.raises(Exception) as excinfo:
pl.scan_csv(Path(__file__).parent.parent / "files" / "empty.csv").collect()
assert str(excinfo.value) == "empty csv"


def test_invalid_utf8() -> None:
np.random.seed(1)
bts = bytes(np.random.randint(0, 255, 200))
Expand Down

0 comments on commit 3c0b1d1

Please sign in to comment.