Skip to content

Extract year from underlying Date representation

Description

Returns the year number in the calendar date.

Usage

<Expr>$dt$year()

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  date = as.Date(c("1977-01-01", "1978-01-01", "1979-01-01"))
)
df$with_columns(
  year = pl$col("date")$dt$year(),
  iso_year = pl$col("date")$dt$iso_year()
)
#> shape: (3, 3)
#> ┌────────────┬──────┬──────────┐
#> │ date       ┆ year ┆ iso_year │
#> │ ---        ┆ ---  ┆ ---      │
#> │ date       ┆ i32  ┆ i32      │
#> ╞════════════╪══════╪══════════╡
#> │ 1977-01-01 ┆ 1977 ┆ 1976     │
#> │ 1978-01-01 ┆ 1978 ┆ 1977     │
#> │ 1979-01-01 ┆ 1979 ┆ 1979     │
#> └────────────┴──────┴──────────┘