Skip to content

Change time unit

Description

Cast the underlying data to another time unit. This may lose precision.

Usage

<Expr>$dt$cast_time_unit(time_unit)

Arguments

time_unit One of “us” (microseconds), “ns” (nanoseconds) or “ms”(milliseconds). Representing the unit of time.

Value

A polars expression

Examples

library("polars")

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