polars.Series.dt.cast_time_unit#

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

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

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", eager=True)
>>> date
shape: (3,)
Series: 'date' [datetime[μs]]
[
        2001-01-01 00:00:00
        2001-01-02 00:00:00
        2001-01-03 00:00:00
]
>>> date.dt.cast_time_unit("ms").alias("time_unit_ms")
shape: (3,)
Series: 'time_unit_ms' [datetime[ms]]
[
        2001-01-01 00:00:00
        2001-01-02 00:00:00
        2001-01-03 00:00:00
]
>>> date.dt.cast_time_unit("ns").alias("time_unit_ns")
shape: (3,)
Series: 'time_unit_ns' [datetime[ns]]
[
        2001-01-01 00:00:00
        2001-01-02 00:00:00
        2001-01-03 00:00:00
]