Skip to content

daft.functions.slice#

slice #

slice(expr: Expression, start: int | Expression, end: int | Expression | None = None) -> Expression

Get a subset of each list or binary value.

Parameters:

Name Type Description Default
expr Expression

List or binary expression to slice.

required
start int | Expression

Index or column of indices. The slice will include elements starting from this index. If start is negative, it represents an offset from the end

required
end int | Expression | None

Index or column of indices. The slice will not include elements from this index onwards. If end is negative, it represents an offset from the end. If not provided, the slice will include elements up to the end of the list. If start > end, an empty slice is produced.

None

Returns:

Name Type Description
Expression Expression

an expression with the same type as the input.

Note

expr[start:stop] is also equivalent to expr.slice(start, stop)

Examples:

Slicing a list expression:

1
2
3
4
>>> import daft
>>> df = daft.from_pydict({"x": [[1, 2, 3], [4, 5, 6, 7], [8]]})
>>> df = df.select(df["x"].slice(1, -1))
>>> df.show()
╭─────────────╮
│ x           │
│ ---         │
│ List[Int64] │
╞═════════════╡
│ [2]         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [5, 6]      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ []          │
╰─────────────╯
(Showing first 3 of 3 rows)

Slicing a binary expression:

1
2
3
>>> df = daft.from_pydict({"x": [b"Hello World", b"\xff\xfe\x00", b"empty"]})
>>> df = df.select(df["x"].slice(1, -2))
>>> df.show()
╭─────────────╮
│ x           │
│ ---         │
│ Binary      │
╞═════════════╡
│ b"ello Wor" │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ b""         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ b"mp"       │
╰─────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/misc.py
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
def slice(expr: Expression, start: int | Expression, end: int | Expression | None = None) -> Expression:
    r"""Get a subset of each list or binary value.

    Args:
        expr: List or binary expression to slice.
        start: Index or column of indices. The slice will include elements starting from this index. If `start` is negative, it represents an offset from the end
        end: Index or column of indices. The slice will not include elements from this index onwards. If `end` is negative, it represents an offset from the end. If not provided, the slice will include elements up to the end of the list. If start > end, an empty slice is produced.

    Returns:
        Expression: an expression with the same type as the input.

    Note:
        `expr[start:stop]` is also equivalent to `expr.slice(start, stop)`

    Examples:
        Slicing a list expression:
        >>> import daft
        >>> df = daft.from_pydict({"x": [[1, 2, 3], [4, 5, 6, 7], [8]]})
        >>> df = df.select(df["x"].slice(1, -1))
        >>> df.show()
        ╭─────────────╮
        │ x           │
        │ ---         │
        │ List[Int64] │
        ╞═════════════╡
        │ [2]         │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [5, 6]      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ []          │
        ╰─────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

        Slicing a binary expression:
        >>> df = daft.from_pydict({"x": [b"Hello World", b"\xff\xfe\x00", b"empty"]})
        >>> df = df.select(df["x"].slice(1, -2))
        >>> df.show()
        ╭─────────────╮
        │ x           │
        │ ---         │
        │ Binary      │
        ╞═════════════╡
        │ b"ello Wor" │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ b""         │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ b"mp"       │
        ╰─────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)
    """
    return Expression._call_builtin_scalar_fn("slice", expr, start, end=end)