Combine Date and Time
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 = c("us", "ns", "ms"))
Arguments
time
|
The number of epoch since or before (if negative) the Date. Can be an Expr or a PTime. |
time_unit
|
One of “us” (default, microseconds), “ns”
(nanoseconds) or “ms” (milliseconds). Representing the unit
of time.
|
Value
A polars expression
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 = hms::parse_hms(c("1:2:3.456000", "7:8:9.101000"))
)
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.456000089 │
#> │ 2023-07-05 23:59:59 ┆ 2022-07-05 12:00:00 GMT ┆ 07:08:09.101000070 │
#> └─────────────────────┴─────────────────────────┴────────────────────┘
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(hms::parse_hms("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 │
#> └─────────────────────────┴─────────────────────────────┴─────────────────────────┘