polars.Expr.dt.ordinal_day#
- Expr.dt.ordinal_day() Expr [source]#
Extract ordinal day from underlying Date representation.
Applies to Date and Datetime columns.
Returns the day of year starting from 1. The return value ranges from 1 to 366. (The last day of year differs by years.)
- Returns:
- Expr
Expression of data type
UInt32
.
Examples
>>> from datetime import timedelta, datetime >>> start = datetime(2001, 12, 22) >>> stop = datetime(2001, 12, 25) >>> df = pl.DataFrame( ... {"date": pl.date_range(start, stop, timedelta(days=1), eager=True)} ... ) >>> df shape: (4, 1) ┌─────────────────────┐ │ date │ │ --- │ │ datetime[μs] │ ╞═════════════════════╡ │ 2001-12-22 00:00:00 │ │ 2001-12-23 00:00:00 │ │ 2001-12-24 00:00:00 │ │ 2001-12-25 00:00:00 │ └─────────────────────┘ >>> df.with_columns( ... pl.col("date").dt.weekday().alias("weekday"), ... pl.col("date").dt.day().alias("day_of_month"), ... pl.col("date").dt.ordinal_day().alias("day_of_year"), ... ) shape: (4, 4) ┌─────────────────────┬─────────┬──────────────┬─────────────┐ │ date ┆ weekday ┆ day_of_month ┆ day_of_year │ │ --- ┆ --- ┆ --- ┆ --- │ │ datetime[μs] ┆ u32 ┆ u32 ┆ u32 │ ╞═════════════════════╪═════════╪══════════════╪═════════════╡ │ 2001-12-22 00:00:00 ┆ 6 ┆ 22 ┆ 356 │ │ 2001-12-23 00:00:00 ┆ 7 ┆ 23 ┆ 357 │ │ 2001-12-24 00:00:00 ┆ 1 ┆ 24 ┆ 358 │ │ 2001-12-25 00:00:00 ┆ 2 ┆ 25 ┆ 359 │ └─────────────────────┴─────────┴──────────────┴─────────────┘