daft.Expression.str.replace

daft.Expression.str.replace#

Expression.str.replace(pattern: str | daft.expressions.expressions.Expression, replacement: str | daft.expressions.expressions.Expression, regex: bool = False) Expression[source]#

Replaces all occurrences of a pattern in a string column with a replacement string. The pattern can be a literal string or a regex pattern.

Example

>>> df = daft.from_pydict({"data": ["foo", "bar", "baz"]})
>>> df.with_column("replace", df["data"].str.replace("ba", "123")).collect()
╭──────┬─────────╮
│ data ┆ replace │
│ ---  ┆ ---     │
│ Utf8 ┆ Utf8    │
╞══════╪═════════╡
│ foo  ┆ foo     │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ bar  ┆ 123r    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ baz  ┆ 123z    │
╰──────┴─────────╯

Replace with a regex pattern

>>> df = daft.from_pydict({"data": ["foo", "fooo", "foooo"]})
>>> df.with_column("replace", df["data"].str.replace(r"o+", "a", regex=True)).collect()
╭───────┬─────────╮
│ data  ┆ replace │
│ ---   ┆ ---     │
│ Utf8  ┆ Utf8    │
╞═══════╪═════════╡
│ foo   ┆ fa      │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ fooo  ┆ fa      │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ foooo ┆ fa      │
╰───────┴─────────╯
Parameters:
  • pattern – The pattern to replace

  • replacement – The replacement string

  • regex – Whether the pattern is a regex pattern or an exact match. Defaults to False.

Returns:

a String expression with patterns replaced by the replacement string

Return type:

Expression