I am a beginner and want to understand if my code is idiomatic polars rust.
I have written the following function:
<code>fn transform_str_to_tb(s: Series) -> Series {
s.iter()
.map(|v| {
let value = v.to_string().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,
"PB" => num * 1000.0,
_ => panic!("Unsupported unit: {}", unit),
}
})
.collect()
}
</code>
<code>fn transform_str_to_tb(s: Series) -> Series {
s.iter()
.map(|v| {
let value = v.to_string().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,
"PB" => num * 1000.0,
_ => panic!("Unsupported unit: {}", unit),
}
})
.collect()
}
</code>
fn transform_str_to_tb(s: Series) -> Series {
s.iter()
.map(|v| {
let value = v.to_string().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,
"PB" => num * 1000.0,
_ => panic!("Unsupported unit: {}", unit),
}
})
.collect()
}
I then call it when operating on a DataFrame as follows:
<code>let df = df
.lazy()
.with_columns([col("value")
.map(
|s| Ok(Some(transform_str_to_tb(s))),
GetOutput::default(),
)
.alias("value_tb")])
.collect()?;
</code>
<code>let df = df
.lazy()
.with_columns([col("value")
.map(
|s| Ok(Some(transform_str_to_tb(s))),
GetOutput::default(),
)
.alias("value_tb")])
.collect()?;
</code>
let df = df
.lazy()
.with_columns([col("value")
.map(
|s| Ok(Some(transform_str_to_tb(s))),
GetOutput::default(),
)
.alias("value_tb")])
.collect()?;
Any recommendations for how this can be improved?