Skip to content

Combine Date and Time

Source code

Description

If the underlying expression is a Datetime then its time component is replaced, and if it is a Date then a new Datetime is created by combining the two values.

Usage

<Expr>$dt$combine(time, time_unit = "us")

Arguments

time The number of epoch since or before (if negative) the Date. Can be an Expr or a PTime.
time_unit Unit of time. One of “ms”, “us” (default) or “ns”.

Value

Date/Datetime expr

Examples

library("polars")

df = pl$DataFrame(
  dtm = c(
    ISOdatetime(2022, 12, 31, 10, 30, 45),
    ISOdatetime(2023, 7, 5, 23, 59, 59)
  ),
  dt = c(ISOdate(2022, 10, 10), ISOdate(2022, 7, 5)),
  tm = c(pl$time(1, 2, 3, 456000), pl$time(7, 8, 9, 101000))
)$explode("tm")

df
#> shape: (2, 3)
#> ┌─────────────────────┬─────────────────────────┬──────────────┐
#> │ dtm                 ┆ dt                      ┆ tm           │
#> │ ---                 ┆ ---                     ┆ ---          │
#> │ datetime[ms]        ┆ datetime[ms, GMT]       ┆ time         │
#> ╞═════════════════════╪═════════════════════════╪══════════════╡
#> │ 2022-12-31 10:30:45 ┆ 2022-10-10 12:00:00 GMT ┆ 01:02:03.456 │
#> │ 2023-07-05 23:59:59 ┆ 2022-07-05 12:00:00 GMT ┆ 07:08:09.101 │
#> └─────────────────────┴─────────────────────────┴──────────────┘
df$select(
  d1 = pl$col("dtm")$dt$combine(pl$col("tm")),
  s2 = pl$col("dt")$dt$combine(pl$col("tm")),
  d3 = pl$col("dt")$dt$combine(pl$time(4, 5, 6))
)
#> shape: (2, 3)
#> ┌─────────────────────────┬─────────────────────────────┬─────────────────────────┐
#> │ d1                      ┆ s2                          ┆ d3                      │
#> │ ---                     ┆ ---                         ┆ ---                     │
#> │ datetime[μs]            ┆ datetime[μs, GMT]           ┆ datetime[μs, GMT]       │
#> ╞═════════════════════════╪═════════════════════════════╪═════════════════════════╡
#> │ 2022-12-31 01:02:03.456 ┆ 2022-10-10 01:02:03.456 GMT ┆ 2022-10-10 04:05:06 GMT │
#> │ 2023-07-05 07:08:09.101 ┆ 2022-07-05 07:08:09.101 GMT ┆ 2022-07-05 04:05:06 GMT │
#> └─────────────────────────┴─────────────────────────────┴─────────────────────────┘