polars.Series.dt.replace_time_zone#

Series.dt.replace_time_zone(time_zone: str | None) Series[source]#

Replace time zone for a Series of type Datetime.

Different from convert_time_zone, this will also modify the underlying timestamp and will ignore the original time zone.

Parameters:
time_zone

Time zone for the Datetime Series. Pass None to unset time zone.

Examples

>>> from datetime import datetime
>>> start = datetime(2020, 3, 1)
>>> stop = datetime(2020, 5, 1)
>>> date = pl.date_range(start, stop, "1mo", time_zone="UTC")
>>> date
shape: (3,)
Series: '' [datetime[μs, UTC]]
[
        2020-03-01 00:00:00 UTC
        2020-04-01 00:00:00 UTC
        2020-05-01 00:00:00 UTC
]
>>> date.dt.epoch(tu="s")
shape: (3,)
Series: '' [i64]
[
        1583020800
        1585699200
        1588291200
]
>>> date = date.dt.convert_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 convert_time_zone
>>> date.dt.epoch(tu="s")
shape: (3,)
Series: 'London' [i64]
[
        1583020800
        1585699200
        1588291200
]
>>> date = date.dt.replace_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 replace_time_zone
>>> date.dt.epoch(tu="s")
shape: (3,)
Series: 'NYC' [i64]
[
    1583038800
    1585717200
    1588309200
]