polars.Series.dt.with_time_unit#

Series.dt.with_time_unit(time_unit: TimeUnit) Series[source]#

Set time unit a Series of dtype Datetime or Duration.

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

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

Unit of time for the Datetime Series.

Examples

>>> from datetime import datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 3)
>>> date = pl.date_range(start, stop, "1d", time_unit="ns", eager=True)
>>> date
shape: (3,)
Series: 'date' [datetime[ns]]
[
        2001-01-01 00:00:00
        2001-01-02 00:00:00
        2001-01-03 00:00:00
]
>>> date.dt.with_time_unit("us").alias("time_unit_us")
shape: (3,)
Series: 'time_unit_us' [datetime[μs]]
[
        +32971-04-28 00:00:00
        +32974-01-22 00:00:00
        +32976-10-18 00:00:00
]