polars.Expr.dt.year#

Expr.dt.year() Expr[source]#

Extract year from underlying Date representation.

Applies to Date and Datetime columns.

Returns the year number in the calendar date.

Returns:
Year as Int32

Examples

>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2002, 7, 1)
>>> df = pl.DataFrame({"date": pl.date_range(start, stop, timedelta(days=180))})
>>> df
shape: (4, 1)
┌─────────────────────┐
│ date                │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-06-30 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-12-27 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2002-06-25 00:00:00 │
└─────────────────────┘
>>> df.select(pl.col("date").dt.year())
shape: (4, 1)
┌──────┐
│ date │
│ ---  │
│ i32  │
╞══════╡
│ 2001 │
├╌╌╌╌╌╌┤
│ 2001 │
├╌╌╌╌╌╌┤
│ 2001 │
├╌╌╌╌╌╌┤
│ 2002 │
└──────┘