Skip to content

daft.functions.is_in#

is_in #

is_in(expr: Expression, other: Iterable[Any] | Expression) -> Expression

Checks if values in the Expression are in the provided iterable.

Parameters:

Name Type Description Default
expr Expression

The expression to check

required
other Iterable[Any] | Expression

An iterable (list, set, tuple, etc.), Expression, or array-like object containing the values to check against

required

Returns:

Name Type Description
Expression Boolean Expression

expression indicating whether values are in the provided iterable

Examples:

1
2
3
4
5
6
>>> import daft
>>> from daft.functions import is_in
>>>
>>> df = daft.from_pydict({"data": [1, 2, 3]})
>>> df = df.select(is_in(df["data"], [1, 3]))
>>> df.collect()
╭───────╮
│ data  │
│ ---   │
│ Bool  │
╞═══════╡
│ true  │
├╌╌╌╌╌╌╌┤
│ false │
├╌╌╌╌╌╌╌┤
│ true  │
╰───────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/misc.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def is_in(expr: Expression, other: Iterable[Any] | Expression) -> Expression:
    """Checks if values in the Expression are in the provided iterable.

    Args:
        expr: The expression to check
        other: An iterable (list, set, tuple, etc.), Expression, or array-like object containing the values to check against

    Returns:
        Expression (Boolean Expression): expression indicating whether values are in the provided iterable

    Examples:
        >>> import daft
        >>> from daft.functions import is_in
        >>>
        >>> df = daft.from_pydict({"data": [1, 2, 3]})
        >>> df = df.select(is_in(df["data"], [1, 3]))
        >>> df.collect()
        ╭───────╮
        │ data  │
        │ ---   │
        │ Bool  │
        ╞═══════╡
        │ true  │
        ├╌╌╌╌╌╌╌┤
        │ false │
        ├╌╌╌╌╌╌╌┤
        │ true  │
        ╰───────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    # Convert non-list iterables (sets, tuples, generators, ranges, etc.) to lists
    # Exclude strings/bytes since they are iterable but should not be treated as sequences of characters/bytes
    if isinstance(other, Iterable) and not isinstance(other, (str, bytes, Expression)):
        other = list(other)

    if isinstance(other, list):
        other = [Expression._to_expression(item) for item in other]
    elif not isinstance(other, Expression):
        series = item_to_series("items", other)
        other = [Expression._from_pyexpr(native.list_lit(series._series))]
    else:
        other = [other]

    expr = Expression._to_expression(expr)
    return Expression._from_pyexpr(expr._expr.is_in([item._expr for item in other]))