CSV
Read & write
Reading a CSV file should look familiar:
df = pl.read_csv("docs/assets/data/path.csv")
CsvReader
· Available on feature csv
use polars::prelude::*;
let df = CsvReadOptions::default()
.try_into_reader_with_file_path(Some("docs/assets/data/path.csv".into()))
.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/assets/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/assets/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/assets/data/path.csv")
LazyCsvReader
· Available on feature csv
let lf = LazyCsvReader::new("./test.csv").finish().unwrap();
If you want to know why this is desirable, you can read more about these Polars optimizations here.