Skip to content

daft.functions.split_part#

split_part #

split_part(expr: Expression, delim: str | Expression, part: int | Expression) -> Expression

Splits the string on occurrences of the delimiter and returns the requested part (1-based).

If part is negative, the parts are counted backward from the end of the string. If part is out of range, an empty string is returned. If part is 0, an error is raised. If the delimiter is an empty string, the string is not split. This is compatible with Spark's split_part function.

Parameters:

Name Type Description Default
expr Expression

The string expression to split

required
delim str | Expression

The delimiter string to split on (not a regular expression)

required
part int | Expression

The 1-based index of the part to return

required

Returns:

Name Type Description
Expression Expression

a String expression with the requested part

Examples:

1
2
3
4
>>> import daft
>>> from daft.functions import split_part
>>> df = daft.from_pydict({"x": ["a,b,c", "x,y,z"]})
>>> df.select(split_part(df["x"], ",", 2)).show()
╭────────╮
│ x      │
│ ---    │
│ String │
╞════════╡
│ b      │
├╌╌╌╌╌╌╌╌┤
│ y      │
╰────────╯
(Showing first 2 of 2 rows)

Negative parts are counted from the end of the string:

1
>>> df.select(split_part(df["x"], ",", -1)).show()
╭────────╮
│ x      │
│ ---    │
│ String │
╞════════╡
│ c      │
├╌╌╌╌╌╌╌╌┤
│ z      │
╰────────╯
(Showing first 2 of 2 rows)
Source code in daft/functions/str.py
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
def split_part(
    expr: Expression,
    delim: str | Expression,
    part: int | Expression,
) -> Expression:
    """Splits the string on occurrences of the delimiter and returns the requested part (1-based).

    If part is negative, the parts are counted backward from the end of the string.
    If part is out of range, an empty string is returned.
    If part is 0, an error is raised.
    If the delimiter is an empty string, the string is not split.
    This is compatible with Spark's split_part function.

    Args:
        expr: The string expression to split
        delim: The delimiter string to split on (not a regular expression)
        part: The 1-based index of the part to return

    Returns:
        Expression: a String expression with the requested part

    Examples:
        >>> import daft
        >>> from daft.functions import split_part
        >>> df = daft.from_pydict({"x": ["a,b,c", "x,y,z"]})
        >>> df.select(split_part(df["x"], ",", 2)).show()
        ╭────────╮
        │ x      │
        │ ---    │
        │ String │
        ╞════════╡
        │ b      │
        ├╌╌╌╌╌╌╌╌┤
        │ y      │
        ╰────────╯
        <BLANKLINE>
        (Showing first 2 of 2 rows)

        Negative parts are counted from the end of the string:

        >>> df.select(split_part(df["x"], ",", -1)).show()
        ╭────────╮
        │ x      │
        │ ---    │
        │ String │
        ╞════════╡
        │ c      │
        ├╌╌╌╌╌╌╌╌┤
        │ z      │
        ╰────────╯
        <BLANKLINE>
        (Showing first 2 of 2 rows)

    """
    return Expression._call_builtin_scalar_fn("split_part", expr, delim, part)