Skip to content

Create a Polars literal expression of type Date

Description

Create a Polars literal expression of type Date

Usage

pl$date(year, month, day)

Arguments

year An polars expression or something can be coerced to an polars expression by as_polars_expr(), which represents a column or literal number of year.
month An polars expression or something can be coerced to an polars expression by as_polars_expr(), which represents a column or literal number of month. Range: 1-12.
day An polars expression or something can be coerced to an polars expression by as_polars_expr(), which represents a column or literal number of day. Range: 1-31.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(month = 1:3, day = 4:6)
df$with_columns(pl$date(2024, pl$col("month"), pl$col("day")))
#> shape: (3, 3)
#> ┌───────┬─────┬────────────┐
#> │ month ┆ day ┆ date       │
#> │ ---   ┆ --- ┆ ---        │
#> │ i32   ┆ i32 ┆ date       │
#> ╞═══════╪═════╪════════════╡
#> │ 1     ┆ 4   ┆ 2024-01-04 │
#> │ 2     ┆ 5   ┆ 2024-02-05 │
#> │ 3     ┆ 6   ┆ 2024-03-06 │
#> └───────┴─────┴────────────┘
# We can also use `pl$date()` for filtering:
df <- pl$DataFrame(
  start = rep(as.Date("2024-01-01"), 3),
  end = as.Date(c("2024-05-01", "2024-07-01", "2024-09-01"))
)
df$filter(pl$col("end") > pl$date(2024, 6, 1))
#> shape: (2, 2)
#> ┌────────────┬────────────┐
#> │ start      ┆ end        │
#> │ ---        ┆ ---        │
#> │ date       ┆ date       │
#> ╞════════════╪════════════╡
#> │ 2024-01-01 ┆ 2024-07-01 │
#> │ 2024-01-01 ┆ 2024-09-01 │
#> └────────────┴────────────┘