Skip to content

daft.functions.length#

length #

length(expr: Expression) -> Expression

Retrieves the length of the given expression.

Parameters:

Name Type Description Default
expr List or Binary or String Expression

expression to compute the length of.

required

The behavior depends on the input type: - For strings, returns the number of characters. - For binary, returns the number of bytes. - For lists, returns the number of elements.

Returns:

Name Type Description
Expression UInt64 Expression

an expression with the length

Examples:

String length:

1
2
3
4
5
6
>>> import daft
>>> from daft.functions import length
>>>
>>> df = daft.from_pydict({"x": ["foo", "bar", None]})
>>> df = df.select(length(df["x"]))
>>> df.show()
╭────────╮
│ x      │
│ ---    │
│ UInt64 │
╞════════╡
│ 3      │
├╌╌╌╌╌╌╌╌┤
│ 3      │
├╌╌╌╌╌╌╌╌┤
│ None   │
╰────────╯
(Showing first 3 of 3 rows)

Binary length:

1
2
3
>>> df = daft.from_pydict({"x": [b"foo", b"bar", None]})
>>> df = df.select(length(df["x"]))
>>> df.show()
╭────────╮
│ x      │
│ ---    │
│ UInt64 │
╞════════╡
│ 3      │
├╌╌╌╌╌╌╌╌┤
│ 3      │
├╌╌╌╌╌╌╌╌┤
│ None   │
╰────────╯
(Showing first 3 of 3 rows)

List length:

1
2
3
>>> df = daft.from_pydict({"x": [[1, 2, 3], [4, 5], None]})
>>> df = df.select(length(df["x"]))
>>> df.show()
╭────────╮
│ x      │
│ ---    │
│ UInt64 │
╞════════╡
│ 3      │
├╌╌╌╌╌╌╌╌┤
│ 2      │
├╌╌╌╌╌╌╌╌┤
│ None   │
╰────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/misc.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
def length(expr: Expression) -> Expression:
    """Retrieves the length of the given expression.

    Args:
        expr (List or Binary or String Expression): expression to compute the length of.

    The behavior depends on the input type:
    - For strings, returns the number of characters.
    - For binary, returns the number of bytes.
    - For lists, returns the number of elements.

    Returns:
        Expression (UInt64 Expression): an expression with the length

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

        Binary length:
        >>> df = daft.from_pydict({"x": [b"foo", b"bar", None]})
        >>> df = df.select(length(df["x"]))
        >>> df.show()
        ╭────────╮
        │ x      │
        │ ---    │
        │ UInt64 │
        ╞════════╡
        │ 3      │
        ├╌╌╌╌╌╌╌╌┤
        │ 3      │
        ├╌╌╌╌╌╌╌╌┤
        │ None   │
        ╰────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

        List length:
        >>> df = daft.from_pydict({"x": [[1, 2, 3], [4, 5], None]})
        >>> df = df.select(length(df["x"]))
        >>> df.show()
        ╭────────╮
        │ x      │
        │ ---    │
        │ UInt64 │
        ╞════════╡
        │ 3      │
        ├╌╌╌╌╌╌╌╌┤
        │ 2      │
        ├╌╌╌╌╌╌╌╌┤
        │ None   │
        ╰────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)
    """
    return Expression._call_builtin_scalar_fn("length", expr)