polars.Expr.dt.replace_time_zone#

Expr.dt.replace_time_zone(time_zone: str | None) Expr[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
>>> df = pl.DataFrame(
...     {
...         "london_timezone": pl.date_range(
...             datetime(2020, 3, 1),
...             datetime(2020, 7, 1),
...             "1mo",
...             time_zone="UTC",
...         ).dt.convert_time_zone(tz="Europe/London"),
...     }
... )
>>> df.select(
...     [
...         pl.col("london_timezone"),
...         pl.col("london_timezone")
...         .dt.replace_time_zone(tz="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       │
└─────────────────────────────┴────────────────────────────────┘