Skip to content

Create subslices of the string values of a String Series

Source code

Description

Create subslices of the string values of a String Series

Usage

<Expr>$str$slice(offset, length = NULL)

Arguments

offset Start index. Negative indexing is supported.
length Length of the slice. If NULL (default), the slice is taken to the end of the string.

Value

Expr: Series of dtype String.

Examples

library("polars")

df = pl$DataFrame(s = c("pear", NA, "papaya", "dragonfruit"))
df$with_columns(
  pl$col("s")$str$slice(-3)$alias("s_sliced")
)
#> shape: (4, 2)
#> ┌─────────────┬──────────┐
#> │ s           ┆ s_sliced │
#> │ ---         ┆ ---      │
#> │ str         ┆ str      │
#> ╞═════════════╪══════════╡
#> │ pear        ┆ ear      │
#> │ null        ┆ null     │
#> │ papaya      ┆ aya      │
#> │ dragonfruit ┆ uit      │
#> └─────────────┴──────────┘