polars.Expr.dt.day#
- Expr.dt.day() Expr [source]#
Extract day from underlying Date representation.
Applies to Date and Datetime columns.
Returns the day of month starting from 1. The return value ranges from 1 to 31. (The last day of month differs by months.)
- Returns:
- Day as UInt32
Examples
>>> from datetime import timedelta, datetime >>> start = datetime(2001, 1, 1) >>> stop = datetime(2001, 1, 9) >>> df = pl.DataFrame({"date": pl.date_range(start, stop, timedelta(days=3))}) >>> df shape: (3, 1) ┌─────────────────────┐ │ date │ │ --- │ │ datetime[μs] │ ╞═════════════════════╡ │ 2001-01-01 00:00:00 │ │ 2001-01-04 00:00:00 │ │ 2001-01-07 00:00:00 │ └─────────────────────┘ >>> df.select( ... [ ... 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: (3, 3) ┌─────────┬──────────────┬─────────────┐ │ weekday ┆ day_of_month ┆ day_of_year │ │ --- ┆ --- ┆ --- │ │ u32 ┆ u32 ┆ u32 │ ╞═════════╪══════════════╪═════════════╡ │ 1 ┆ 1 ┆ 1 │ │ 4 ┆ 4 ┆ 4 │ │ 7 ┆ 7 ┆ 7 │ └─────────┴──────────────┴─────────────┘