Skip to content

Replace time unit

Source code

Description

Replace time unit

Usage

<Expr>$dt$replace(
  ...,
  year = NULL,
  month = NULL,
  day = NULL,
  hour = NULL,
  minute = NULL,
  second = NULL,
  microsecond = NULL,
  ambiguous = c("raise", "earliest", "latest", "null")
)

Arguments

These dots are for future extensions and must be empty.
year Column or literal.
month Column or literal, ranging from 1-12.
day Column or literal, ranging from 1-31
hour Column or literal, ranging from 0-23.
minute Column or literal, ranging from 0-59.
second Column or literal, ranging from 0-59.
microsecond Column or literal, ranging from 0-999999.
ambiguous Determine how to deal with ambiguous datetimes. Character vector or expression containing the followings:
  • “raise” (default): Throw an error
  • “earliest”: Use the earliest datetime
  • “latest”: Use the latest datetime
  • “null”: Return a null value

Value

A polars expression

Examples

library("polars")

df = pl$DataFrame(
  date = as.Date(c("2024-04-01", "2025-03-16")),
  new_day = c(10, 15)
)
df$with_columns(replaced = pl$col("date")$dt$replace(day = "new_day"))
#> shape: (2, 3)
#> ┌────────────┬─────────┬────────────┐
#> │ date       ┆ new_day ┆ replaced   │
#> │ ---        ┆ ---     ┆ ---        │
#> │ date       ┆ f64     ┆ date       │
#> ╞════════════╪═════════╪════════════╡
#> │ 2024-04-01 ┆ 10.0    ┆ 2024-04-10 │
#> │ 2025-03-16 ┆ 15.0    ┆ 2025-03-15 │
#> └────────────┴─────────┴────────────┘