polars.Expr.dt.with_time_unit#

Expr.dt.with_time_unit(tu: TimeUnit) Expr[source]#

Set time unit of a Series of dtype Datetime or Duration.

This does not modify underlying data, and should be used to fix an incorrect time unit.

Parameters:
tu{‘ns’, ‘us’, ‘ms’}

Time unit for the Datetime Series.

Examples

>>> from datetime import datetime
>>> df = pl.DataFrame(
...     {
...         "date": pl.date_range(
...             datetime(2001, 1, 1), datetime(2001, 1, 3), "1d", time_unit="ns"
...         )
...     }
... )
>>> df.select(
...     [
...         pl.col("date"),
...         pl.col("date").dt.with_time_unit(tu="us").alias("tu_us"),
...     ]
... )
shape: (3, 2)
┌─────────────────────┬───────────────────────┐
│ date                ┆ tu_us                 │
│ ---                 ┆ ---                   │
│ datetime[ns]        ┆ datetime[μs]          │
╞═════════════════════╪═══════════════════════╡
│ 2001-01-01 00:00:00 ┆ +32971-04-28 00:00:00 │
│ 2001-01-02 00:00:00 ┆ +32974-01-22 00:00:00 │
│ 2001-01-03 00:00:00 ┆ +32976-10-18 00:00:00 │
└─────────────────────┴───────────────────────┘