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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
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)