My problem can probably be described as being very new to both Rust and Polars. Go easy on me. :)
I'm trying to establish a pattern using custom functions, based on this documentation: https://pola-rs.github.io/polars-book/user-guide/dsl/custom_functions.html, however am so far unsuccessful.
In my code, I have a function declared as follows:
pub fn convert_str_to_tb(value: &str) -> f64 {
let value = value.replace(",", "");
let mut parts = value.split_whitespace();
let num = parts.next().unwrap().parse::<f64>().unwrap();
let unit = parts.next().unwrap();
match unit {
"KB" => num / (1000.0 * 1000.0 * 1000.0),
"MB" => num / (1000.0 * 1000.0),
"GB" => num / 1000.0,
"TB" => num,
_ => panic!("Unsupported unit: {}", unit),
}
}
I believe I should be able to call this function like so:
df.with_columns([
col("value").map(|s| Ok(convert_str_to_tb(s))).alias("value_tb");
])
My first issue was that with_columns method doesn't seem to exist - I had to use with_column. If I use the with_column, I receive the following error:
the trait bound `Expr: IntoSeries` is not satisfied
the following other types implement trait `IntoSeries`:
Arc<(dyn polars::prelude::SeriesTrait + 'static)>
ChunkedArray<T>
Logical<DateType, Int32Type>
Logical<DatetimeType, Int64Type>
Logical<DurationType, Int64Type>
Logical<TimeType, Int64Type>
polars::prelude::SeriesrustcClick for full compiler diagnostic
The DataFrame I am trying to transform:
let mut df = df!("volume" => &["volume01", "volume02", "volume03"],
"value" => &["1,000 GB", "2,000,000 MB", "3 TB"]).unwrap();
Perhaps there is a way to do this without a custom function?