1

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?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Jage
  • 453
  • 2
  • 9

1 Answers1

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

Is more idiomatic and about 5x faster.

Jage
  • 453
  • 2
  • 9