Skip to content

string_split

laktory.narwhals_ext.expr.string_split ¤

FUNCTION DESCRIPTION
string_split

Get substring using separator pattern.

string_split(self, pattern, key) ¤

Get substring using separator pattern.

PARAMETER DESCRIPTION
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:

```py¤

import polars as pl¤

¤

import laktory as lk # noqa: F401¤

¤

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

¤

df = df.with_columns(y=nw.col("x").laktory.string_split(pattern="_", key=1))¤

print(df.to_native().glimpse(return_as_string=True))¤

'''¤

Rows: 1¤

Columns: 2¤

$ x 'price_close'¤

$ y 'close'¤

'''¤

```

Source code in laktory/narwhals_ext/expr/string_split.py
 4
 5
 6
 7
 8
 9
10
11
12
13
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
def string_split(
    self,
    pattern: str,
    key: int,
) -> nw.Expr:
    """
    Get substring using separator `pattern`.

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

    Returns
    -------
    :
        Result

    Examples
    --------
    # ```py
    # import polars as pl
    #
    # import laktory as lk  # noqa: F401
    #
    # df = nw.from_native(pl.DataFrame({"x": ["price_close"]}))
    #
    # df = df.with_columns(y=nw.col("x").laktory.string_split(pattern="_", key=1))
    # print(df.to_native().glimpse(return_as_string=True))
    # '''
    # Rows: 1
    # Columns: 2
    # $ x <str> 'price_close'
    # $ y <str> 'close'
    # '''
    ```
    """
    raise NotImplementedError()
    return self._expr.str.split(by=pattern).list.get(key, null_on_oob=True)