CSV
Read & write
Reading a CSV file should look familiar:
df = pl.read_csv("docs/data/path.csv")
CsvReader
· Available on feature csv
use polars::prelude::*;
let df = CsvReader::from_path("docs/data/path.csv").unwrap().finish().unwrap();
Writing a CSV file is similar with the write_csv
function:
df = pl.DataFrame({"foo": [1, 2, 3], "bar": [None, "bak", "baz"]})
df.write_csv("docs/data/path.csv")
CsvWriter
· Available on feature csv
let mut df = df!(
"foo" => &[1, 2, 3],
"bar" => &[None, Some("bak"), Some("baz")],
)
.unwrap();
let mut file = std::fs::File::create("docs/data/path.csv").unwrap();
CsvWriter::new(&mut file).finish(&mut df).unwrap();
Scan
Polars
allows you to scan a CSV input. Scanning delays the actual parsing of the
file and instead returns a lazy computation holder called a LazyFrame
.
df = pl.scan_csv("docs/data/path.csv")
LazyCsvReader
· Available on feature csv
let df = LazyCsvReader::new("./test.csv").finish().unwrap();
If you want to know why this is desirable, you can read more about these Polars
optimizations here.