Search code examples
rust-polars

Use replace_all() method in polars dataframe column


I'm trying to translate this polars-py code into polars-rs:

# python
df = df.with_columns(
    pl.col("my_text_column").str\
        .replace_all('some_literal', pl.col("my_other_text_column"), literal=True)
)
-- Rust
df = df.lazy().with_column(
    col("my_text_column").str()
    .replace_all(lit("some_literal"), col("my_other_text_column"), true)
).collect()?;

However, I get the error


error[E0599]: no method named `replace_all` found for struct `StringNameSpace` in the current scope
  --> src/alert_replicate.rs:38:10
   |
37 | /         col("my_text_column").str()
38 | |         .replace_all(lit("some_literal"), col("my_other_text_column"), true)
   | |         -^^^^^^^^^^^ method not found in `StringNameSpace`
   | |_________|
   | 

The documentation for polars::prelude::StringNameSpaceImpl::replace_all() is not too enlightening.

Any help would be appreciated.


Solution

  • I looked in the source code for polars-plan/src/dsl/string.rs and found out the replace_all method requires the feature regex.

    cargo add polars --features regex solved my problem. Adding this info to the documentation would be helpful.