polars.Series.dt.cast_time_zone#

Series.dt.cast_time_zone(tz: str) Series[source]#

Cast time zone for a Series of type Datetime.

Different from with_time_zone, this will also modify the underlying timestamp.

Parameters:
tz

Time zone for the Datetime Series.

Examples

>>> from datetime import datetime
>>> start = datetime(2020, 3, 1)
>>> stop = datetime(2020, 5, 1)
>>> date = pl.date_range(start, stop, "1mo")
>>> date
shape: (3,)
Series: '' [datetime[μs]]
[
        2020-03-01 00:00:00
        2020-04-01 00:00:00
        2020-05-01 00:00:00
]
>>> date.dt.epoch(tu="s")
shape: (3,)
Series: '' [i64]
[
        1583020800
        1585699200
        1588291200
]
>>> date = date.dt.with_time_zone(tz="Europe/London").alias("London")
>>> date
shape: (3,)
Series: 'London' [datetime[μs, Europe/London]]
[
    2020-03-01 00:00:00 GMT
    2020-04-01 01:00:00 BST
    2020-05-01 01:00:00 BST
]
>>> # Timestamps have not changed after with_time_zone
>>> date.dt.epoch(tu="s")
shape: (3,)
Series: 'London' [i64]
[
        1583020800
        1585699200
        1588291200
]
>>> date = date.dt.cast_time_zone(tz="America/New_York").alias("NYC")
>>> date
shape: (3,)
Series: 'NYC' [datetime[μs, America/New_York]]
[
    2020-03-01 00:00:00 EST
    2020-04-01 01:00:00 EDT
    2020-05-01 01:00:00 EDT
]
>>> # Timestamps have changed after cast_time_zone
>>> date.dt.epoch(tu="s")
shape: (3,)
Series: 'NYC' [i64]
[
    1583038800
    1585717200
    1588309200
]