[go: up one dir, main page]

Skip to content

daft.functions.find#

find #

find(expr: Expression, substr: str | Expression) -> Expression

Returns the index of the first occurrence of the substring in each string.

Returns:

Name Type Description
Expression Expression

an Int64 expression with the index of the first occurrence of the substring in each string

Note

The returned index is 0-based. If the substring is not found, -1 is returned.

Examples:

1
2
3
4
>>> import daft
>>> df = daft.from_pydict({"x": ["daft", "query daft", "df_daft"]})
>>> df = df.select(df["x"].find("daft"))
>>> df.show()
╭───────╮
│ x     │
│ ---   │
│ Int64 │
╞═══════╡
│ 0     │
├╌╌╌╌╌╌╌┤
│ 6     │
├╌╌╌╌╌╌╌┤
│ 3     │
╰───────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
def find(expr: Expression, substr: str | Expression) -> Expression:
    """Returns the index of the first occurrence of the substring in each string.

    Returns:
        Expression: an Int64 expression with the index of the first occurrence of the substring in each string

    Note:
        The returned index is 0-based. If the substring is not found, -1 is returned.

    Examples:
        >>> import daft
        >>> df = daft.from_pydict({"x": ["daft", "query daft", "df_daft"]})
        >>> df = df.select(df["x"].find("daft"))
        >>> df.show()
        ╭───────╮
        │ x     │
        │ ---   │
        │ Int64 │
        ╞═══════╡
        │ 0     │
        ├╌╌╌╌╌╌╌┤
        │ 6     │
        ├╌╌╌╌╌╌╌┤
        │ 3     │
        ╰───────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("find", expr, substr)