polars.Series.dt.replace_time_zone#

Series.dt.replace_time_zone(
time_zone: str | None,
*,
use_earliest: bool | None = 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.

use_earliest

Determine how to deal with ambiguous datetimes:

  • None (default): raise

  • True: use the earliest datetime

  • False: use the latest datetime

Examples

>>> from datetime import datetime
>>> df = pl.DataFrame(
...     {
...         "london_timezone": pl.date_range(
...             datetime(2020, 3, 1),
...             datetime(2020, 7, 1),
...             "1mo",
...             time_zone="UTC",
...             eager=True,
...         ).dt.convert_time_zone(time_zone="Europe/London"),
...     }
... )
>>> df.select(
...     [
...         pl.col("london_timezone"),
...         pl.col("london_timezone")
...         .dt.replace_time_zone(time_zone="Europe/Amsterdam")
...         .alias("London_to_Amsterdam"),
...     ]
... )
shape: (5, 2)
┌─────────────────────────────┬────────────────────────────────┐
│ london_timezone             ┆ London_to_Amsterdam            │
│ ---                         ┆ ---                            │
│ datetime[μs, Europe/London] ┆ datetime[μs, Europe/Amsterdam] │
╞═════════════════════════════╪════════════════════════════════╡
│ 2020-03-01 00:00:00 GMT     ┆ 2020-03-01 00:00:00 CET        │
│ 2020-04-01 01:00:00 BST     ┆ 2020-04-01 01:00:00 CEST       │
│ 2020-05-01 01:00:00 BST     ┆ 2020-05-01 01:00:00 CEST       │
│ 2020-06-01 01:00:00 BST     ┆ 2020-06-01 01:00:00 CEST       │
│ 2020-07-01 01:00:00 BST     ┆ 2020-07-01 01:00:00 CEST       │
└─────────────────────────────┴────────────────────────────────┘

You can use use_earliest to deal with ambiguous datetimes:

>>> dates = [
...     "2018-10-28 01:30",
...     "2018-10-28 02:00",
...     "2018-10-28 02:30",
...     "2018-10-28 02:00",
...     "2018-10-28 02:30",
... ]
>>> df = pl.DataFrame(
...     {
...         "ts": pl.Series(dates).str.strptime(pl.Datetime),
...         "DST": [True, True, True, False, False],
...     }
... )
>>> df.with_columns(
...     ts_localized=pl.when(pl.col("DST"))
...     .then(
...         pl.col("ts").dt.replace_time_zone(
...             "Europe/Brussels", use_earliest=True
...         )
...     )
...     .otherwise(
...         pl.col("ts").dt.replace_time_zone(
...             "Europe/Brussels", use_earliest=False
...         )
...     )
... )
shape: (5, 3)
┌─────────────────────┬───────┬───────────────────────────────┐
│ ts                  ┆ DST   ┆ ts_localized                  │
│ ---                 ┆ ---   ┆ ---                           │
│ datetime[μs]        ┆ bool  ┆ datetime[μs, Europe/Brussels] │
╞═════════════════════╪═══════╪═══════════════════════════════╡
│ 2018-10-28 01:30:00 ┆ true  ┆ 2018-10-28 01:30:00 CEST      │
│ 2018-10-28 02:00:00 ┆ true  ┆ 2018-10-28 02:00:00 CEST      │
│ 2018-10-28 02:30:00 ┆ true  ┆ 2018-10-28 02:30:00 CEST      │
│ 2018-10-28 02:00:00 ┆ false ┆ 2018-10-28 02:00:00 CET       │
│ 2018-10-28 02:30:00 ┆ false ┆ 2018-10-28 02:30:00 CET       │
└─────────────────────┴───────┴───────────────────────────────┘