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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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]))