Skip to content

daft.functions.contains#

contains #

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

Checks whether each string contains the given substring in a string column.

Parameters:

Name Type Description Default
expr Expression

The expression to check.

required
substr str | Expression

The substring to search for as a literal string, or as a column to pick values from

required

Returns:

Name Type Description
Expression Expression

a Boolean expression indicating whether each value contains the provided substring

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import contains
>>> df = daft.from_pydict({"x": ["foo", "bar", "baz"]})
>>> df = df.select(contains(df["x"], "o"))
>>> df.show()
╭───────╮
│ x     │
│ ---   │
│ Bool  │
╞═══════╡
│ true  │
├╌╌╌╌╌╌╌┤
│ false │
├╌╌╌╌╌╌╌┤
│ false │
╰───────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
def contains(expr: Expression, substr: str | Expression) -> Expression:
    """Checks whether each string contains the given substring in a string column.

    Args:
        expr: The expression to check.
        substr: The substring to search for as a literal string, or as a column to pick values from

    Returns:
        Expression: a Boolean expression indicating whether each value contains the provided substring

    Examples:
        >>> import daft
        >>> from daft.functions import contains
        >>> df = daft.from_pydict({"x": ["foo", "bar", "baz"]})
        >>> df = df.select(contains(df["x"], "o"))
        >>> df.show()
        ╭───────╮
        │ x     │
        │ ---   │
        │ Bool  │
        ╞═══════╡
        │ true  │
        ├╌╌╌╌╌╌╌┤
        │ false │
        ├╌╌╌╌╌╌╌┤
        │ false │
        ╰───────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

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