Skip to content

Get a slice of this expression

Description

Get a slice of this expression

Usage

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

Arguments

offset Numeric or expression, zero-indexed. Indicates where to start the slice. A negative value is one-indexed and starts from the end.
length Maximum number of elements contained in the slice. If NULL (default), all rows starting at the offset will be selected.

Value

A polars expression

Examples

library("polars")

# as head
pl$DataFrame(a = 0:100)$select(
  pl$all()$slice(0, 6)
)
#> shape: (6, 1)
#> ┌─────┐
#> │ a   │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 0   │
#> │ 1   │
#> │ 2   │
#> │ 3   │
#> │ 4   │
#> │ 5   │
#> └─────┘
# as tail
pl$DataFrame(a = 0:100)$select(
  pl$all()$slice(-6, 6)
)
#> shape: (6, 1)
#> ┌─────┐
#> │ a   │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 95  │
#> │ 96  │
#> │ 97  │
#> │ 98  │
#> │ 99  │
#> │ 100 │
#> └─────┘
pl$DataFrame(a = 0:100)$select(
  pl$all()$slice(80)
)
#> shape: (21, 1)
#> ┌─────┐
#> │ a   │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 80  │
#> │ 81  │
#> │ 82  │
#> │ 83  │
#> │ 84  │
#> │ …   │
#> │ 96  │
#> │ 97  │
#> │ 98  │
#> │ 99  │
#> │ 100 │
#> └─────┘