Skip to content

string_split

laktory.polars.expressions.string_split ยค

string_split(x, pattern, key)

Get substring using separator pat.

PARAMETER DESCRIPTION
x

Input text series to split

TYPE: Expr

pattern

String or regular expression to split on. If not specified, split on whitespace.

TYPE: str

key

Split index to return

TYPE: int

RETURNS DESCRIPTION
Expr

Result

Examples:

import laktory  # noqa: F401
import polars as pl

df = pl.DataFrame({"x": ["price_close"]})

df = df.with_columns(y=pl.Expr.laktory.string_split(pl.col("x"), pattern="_", key=1))
print(df.glimpse(return_as_string=True))
'''
Rows: 1
Columns: 2
$ x <str> 'price_close'
$ y <str> 'close'
'''
Source code in laktory/polars/expressions/string.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def string_split(
    x: pl.Expr,
    pattern: str,
    key: int,
) -> pl.Expr:
    """
    Get substring using separator `pat`.

    Parameters
    ----------
    x:
        Input text series to split
    pattern:
        String or regular expression to split on. If not specified, split on whitespace.
    key:
        Split index to return

    Returns
    -------
    :
        Result

    Examples
    --------
    ```py
    import laktory  # noqa: F401
    import polars as pl

    df = pl.DataFrame({"x": ["price_close"]})

    df = df.with_columns(y=pl.Expr.laktory.string_split(pl.col("x"), pattern="_", key=1))
    print(df.glimpse(return_as_string=True))
    '''
    Rows: 1
    Columns: 2
    $ x <str> 'price_close'
    $ y <str> 'close'
    '''
    ```
    """
    return x.str.split(by=pattern).list.get(key, null_on_oob=True)