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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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)