Search code examples
rustrust-polars

Fast way to concatenate two string columns in Rust Polars?


I am currently concatenating two series in Polars like this:

df.with_column(Series::new(
    "C",
    &(df.column("A").unwrap()
        + &Series::new("", (0..df.shape().0).map(|_| "_").collect::<Vec<&str>>()))
        + df.column("B").unwrap(),
))
.unwrap();
df.with_column(Series::new(
    "E",
    &(df.column("C").unwrap()
        + &Series::new("", (0..df.shape().0).map(|_| "_").collect::<Vec<&str>>()))
        + df.column("D").unwrap(),
))
.unwrap();

But am finding it to be quite slow. Is there a faster way to take two columns, and concatenate them elementwise with a separator?


Solution

  • df
        .lazy()
        .select([
            all(),
            concat_str([col("A"), col("B")], "_").alias("C")
        ])
        .collect()
        .unwrap();
    

    Is more idiomatic and about 5x faster.