Skip to content

Instantly share code, notes, and snippets.

@Steboss89
Created June 28, 2021 14:39
Show Gist options
  • Save Steboss89/f786a90a8ea66ab47a01643e8e82ec4a to your computer and use it in GitHub Desktop.
Save Steboss89/f786a90a8ea66ab47a01643e8e82ec4a to your computer and use it in GitHub Desktop.
Read a csv file and return a dataframe
use polars::prelude::*;//{CsvReader, DataType, Field, Result as PolarResult, Schema, DataFrame,};
use polars::prelude::{Result as PolarResult};
use polars::frame::DataFrame;
use std::fs::File;
use std::path::{Path};
// an eye on traits:
use polars::prelude::SerReader;
// If we don't import this one we might get the error CsvReader::new(file) new function not found in CsvReader
pub fn read_csv<P: AsRef<Path>>(path: P) -> PolarResult<DataFrame> {
/* Example function to create a dataframe from an input csv file*/
let file = File::open(path).expect("Cannot open file.");
CsvReader::new(file)
.has_header(true)
.finish()
}
fn main(){
let ifile = "PATHTODATASET/iris.csv";
let df = read_csv(&ifile).unwrap;
println!{"{}", df.head(Some(5)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment