I've been trying to figure out how to perform this with the Rust Polars library. But I am still trying to learn Rust and its Polars. And the casting is holding me back. With this code, I get an error that:
error[E0599]: no method named `utf8` found for enum `polars::prelude::Column` in the current scope
And changing that to u8()?
makes the issue more broad.
Here is what I am trying to figure out:
use polars::prelude::*;
fn main() -> Result<(), PolarsError> {
let df = df![
"name" => ["Alice", "Bob", "Charlie"],
"age" => [25, 30, 35]
]?;
let df = df
.lazy()
.with_column(
col("name")
.map(
|name_series| {
name_series
.utf8()?
.into_iter()
.map(|name| name.map(|n| format!("Hello, {}!", n)))
.collect::<Vec<_>>()
.into_iter()
},
GetOutput::same_type(),
)
.alias("greeting"),
)
.collect()?;
println!("{:?}", df);
Ok(())
}
This would be the desired output:
name age greeting
str i32 str
-------------------------------------
Alice 25 Hello, Alice!
Bob 30 Hello, Bob!
Charlie 35 Hello, Charlie!
You need feature strings
enabled, and the method is called str()
not utf8()
in more recent versions of polars.
Then you'll also need to collect into a StringChunked
and call into_column()
on that, and finally wrap the result of the closure in Ok(Some(...))
.
.map(
|name_series| {
Ok(Some(
name_series
.str()?
.into_iter()
.map(|name| {
name.map(|n| format!("Hello, {}!", n))
})
.collect::<StringChunked>()
.into_column(),
))
},
GetOutput::same_type(),
)