Skip to content

Select all duration columns, optionally filtering by time unit

Description

Select all duration columns, optionally filtering by time unit

Usage

cs__duration(time_unit = c("ms", "us", "ns"))

Arguments

time_unit One (or more) of the allowed time unit precision strings, “ms”, “us”, and “ns”. Default is to select columns with any valid timeunit.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")


df <- pl$DataFrame(
  dtm = as.POSIXct(c("2001-5-7 10:25", "2031-12-31 00:30")),
  dur_ms = clock::duration_milliseconds(1:2),
  dur_us = clock::duration_microseconds(1:2),
  dur_ns = clock::duration_nanoseconds(1:2),
)

# Select duration columns:
df$select(cs$duration())
#> shape: (2, 3)
#> ┌──────────────┬──────────────┬──────────────┐
#> │ dur_ms       ┆ dur_us       ┆ dur_ns       │
#> │ ---          ┆ ---          ┆ ---          │
#> │ duration[ms] ┆ duration[μs] ┆ duration[ns] │
#> ╞══════════════╪══════════════╪══════════════╡
#> │ 1ms          ┆ 1µs          ┆ 1ns          │
#> │ 2ms          ┆ 2µs          ┆ 2ns          │
#> └──────────────┴──────────────┴──────────────┘
# Select all duration columns that have "ms" precision:
df$select(cs$duration("ms"))
#> shape: (2, 1)
#> ┌──────────────┐
#> │ dur_ms       │
#> │ ---          │
#> │ duration[ms] │
#> ╞══════════════╡
#> │ 1ms          │
#> │ 2ms          │
#> └──────────────┘
# Select all duration columns that have "ms" OR "ns" precision:
df$select(cs$duration(c("ms", "ns")))
#> shape: (2, 2)
#> ┌──────────────┬──────────────┐
#> │ dur_ms       ┆ dur_ns       │
#> │ ---          ┆ ---          │
#> │ duration[ms] ┆ duration[ns] │
#> ╞══════════════╪══════════════╡
#> │ 1ms          ┆ 1ns          │
#> │ 2ms          ┆ 2ns          │
#> └──────────────┴──────────────┘
# Select all columns except for those that are duration:
df$select(!cs$duration())
#> shape: (2, 1)
#> ┌─────────────────────┐
#> │ dtm                 │
#> │ ---                 │
#> │ datetime[ms]        │
#> ╞═════════════════════╡
#> │ 2001-05-07 10:25:00 │
#> │ 2031-12-31 00:30:00 │
#> └─────────────────────┘