Skip to content

daft.functions.translate#

translate #

translate(expr: Expression, from_str: str | Expression, to_str: str | Expression) -> Expression

Translates characters in the input string by replacing characters in 'from_str' with corresponding characters in 'to_str'.

Characters in 'from_str' without a corresponding character in 'to_str' are removed. This is compatible with Spark's translate function.

Parameters:

Name Type Description Default
expr Expression

The string expression to translate

required
from_str str | Expression

Characters to be replaced

required
to_str str | Expression

Replacement characters

required

Returns:

Name Type Description
Expression Expression

a String expression with characters translated

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import translate
>>> df = daft.from_pydict({"x": ["AaBbCc", "hello", "world"]})
>>> df = df.select(translate(df["x"], "abc", "123"))
>>> df.show()
╭────────╮
│ x      │
│ ---    │
│ String │
╞════════╡
│ A1B2C3 │
├╌╌╌╌╌╌╌╌┤
│ hello  │
├╌╌╌╌╌╌╌╌┤
│ world  │
╰────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
def translate(
    expr: Expression,
    from_str: str | Expression,
    to_str: str | Expression,
) -> Expression:
    """Translates characters in the input string by replacing characters in 'from_str' with corresponding characters in 'to_str'.

    Characters in 'from_str' without a corresponding character in 'to_str' are removed.
    This is compatible with Spark's translate function.

    Args:
        expr: The string expression to translate
        from_str: Characters to be replaced
        to_str: Replacement characters

    Returns:
        Expression: a String expression with characters translated

    Examples:
        >>> import daft
        >>> from daft.functions import translate
        >>> df = daft.from_pydict({"x": ["AaBbCc", "hello", "world"]})
        >>> df = df.select(translate(df["x"], "abc", "123"))
        >>> df.show()
        ╭────────╮
        │ x      │
        │ ---    │
        │ String │
        ╞════════╡
        │ A1B2C3 │
        ├╌╌╌╌╌╌╌╌┤
        │ hello  │
        ├╌╌╌╌╌╌╌╌┤
        │ world  │
        ╰────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("translate", expr, from_str, to_str)