I'm trying to write a Rust program which uses Polars to read a CSV. This particular CSV encodes an array of floats as a string.
In my program, I want to load the CSV into a DataFrame and then parse this column into an array of floats. In Python you might write code that looks like this:
df = pd.read_csv("...")
df["babbage_search"] = df.babbage_search.apply(eval).apply(np.array)
However in my Rust program it's not clear how to go about this. I could take an approach like this:
let mut df = CsvReader::from_path("...")?
.has_header(true)
.finish()?;
df.apply("babbage_search", parse_vector)?;
However the parse_vector
function is not really clear. I might write something like this, but this won't compile:
fn parse_vector(series: &Series) -> Series {
series
.utf8()
.unwrap()
.into_iter()
.map(|opt_v| match opt_v {
Some(v) => {
let vec: Vec<f64> = serde_json::from_str(v).unwrap();
let series: Series = vec.iter().collect();
series.f64().unwrap().to_owned() as ChunkedArray<Float64Type>
}
None => ChunkedArray::<Float64Type>::default(),
})
.collect()
}
I'd appreciate any help in figuring this out.