Skip to content

daft.functions.ascii_func#

ascii_func #

ascii_func(expr: Expression) -> Expression

Returns the ASCII numeric value of the first character of the string.

Returns 0 for empty strings. This is compatible with Spark's ascii function.

Parameters:

Name Type Description Default
expr Expression

The string expression

required

Returns:

Name Type Description
Expression Expression

an Int32 expression with the ASCII value

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import ascii_func
>>> df = daft.from_pydict({"x": ["A", "abc", ""]})
>>> df = df.select(ascii_func(df["x"]))
>>> df.show()
╭───────╮
│ x     │
│ ---   │
│ Int32 │
╞═══════╡
│ 65    │
├╌╌╌╌╌╌╌┤
│ 97    │
├╌╌╌╌╌╌╌┤
│ 0     │
╰───────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
def ascii_func(expr: Expression) -> Expression:
    """Returns the ASCII numeric value of the first character of the string.

    Returns 0 for empty strings. This is compatible with Spark's ascii function.

    Args:
        expr: The string expression

    Returns:
        Expression: an Int32 expression with the ASCII value

    Examples:
        >>> import daft
        >>> from daft.functions import ascii_func
        >>> df = daft.from_pydict({"x": ["A", "abc", ""]})
        >>> df = df.select(ascii_func(df["x"]))
        >>> df.show()
        ╭───────╮
        │ x     │
        │ ---   │
        │ Int32 │
        ╞═══════╡
        │ 65    │
        ├╌╌╌╌╌╌╌┤
        │ 97    │
        ├╌╌╌╌╌╌╌┤
        │ 0     │
        ╰───────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("ascii", expr)