polars.Expr.dt.cast_time_unit#

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

Cast the underlying data to another time unit. This may lose precision.

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"
...         )
...     }
... )
>>> df.select(
...     [
...         pl.col("date"),
...         pl.col("date").dt.cast_time_unit(tu="ms").alias("tu_ms"),
...         pl.col("date").dt.cast_time_unit(tu="ns").alias("tu_ns"),
...     ]
... )
shape: (3, 3)
┌─────────────────────┬─────────────────────┬─────────────────────┐
│ date                ┆ tu_ms               ┆ tu_ns               │
│ ---                 ┆ ---                 ┆ ---                 │
│ datetime[μs]        ┆ datetime[ms]        ┆ datetime[ns]        │
╞═════════════════════╪═════════════════════╪═════════════════════╡
│ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-02 00:00:00 ┆ 2001-01-02 00:00:00 ┆ 2001-01-02 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-03 00:00:00 ┆ 2001-01-03 00:00:00 ┆ 2001-01-03 00:00:00 │
└─────────────────────┴─────────────────────┴─────────────────────┘