Skip to content

cast_time_unit

Source code

Description

Cast the underlying data to another time unit. This may lose precision. The corresponding global timepoint will stay unchanged +/- precision.

Usage

<Expr>$dt$cast_time_unit(tu = c("ns", "us", "ms"))

Arguments

tu string option either ‘ns’, ‘us’, or ‘ms’

Value

Expr of i64

Examples

library("polars")

df = pl$DataFrame(
  date = pl$datetime_range(
    start = as.Date("2001-1-1"),
    end = as.Date("2001-1-3"),
    interval = "1d1s"
  )
)
df$select(
  pl$col("date"),
  pl$col("date")$dt$cast_time_unit()$alias("cast_time_unit_ns"),
  pl$col("date")$dt$cast_time_unit(tu = "ms")$alias("cast_time_unit_ms")
)
#> shape: (2, 3)
#> ┌─────────────────────┬─────────────────────┬─────────────────────┐
#> │ date                ┆ cast_time_unit_ns   ┆ cast_time_unit_ms   │
#> │ ---                 ┆ ---                 ┆ ---                 │
#> │ datetime[μs]        ┆ datetime[ns]        ┆ datetime[ms]        │
#> ╞═════════════════════╪═════════════════════╪═════════════════════╡
#> │ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 │
#> │ 2001-01-02 00:00:01 ┆ 2001-01-02 00:00:01 ┆ 2001-01-02 00:00:01 │
#> └─────────────────────┴─────────────────────┴─────────────────────┘