Convert to given time zone for an expression of type Datetime.
Time zone for the Datetime expression.
Expr Expression of data type Datetime with the given time zone.
--------
>>> DataFrame([
Series("london_timezone", [new Date(Date.UTC(2026, 0, 1, 6, 0, 0))],DataType.Datetime("us"),).dt.replaceTimeZone("Europe/London"),
]).select([
pl.col("london_timezone"),
pl.col("london_timezone").dt.convertTimeZone("Europe/Amsterdam").alias("London_to_Amsterdam"),
]);
shape: (1, 2)
┌─────────────────────────────┬────────────────────────────────┐
│ london_timezone ┆ London_to_Amsterdam │
│ --- ┆ --- │
│ datetime[μs, Europe/London] ┆ datetime[μs, Europe/Amsterdam] │
╞═════════════════════════════╪════════════════════════════════╡
│ 2026-01-01 06:00:00 GMT ┆ 2026-01-01 07:00:00 CET │
└─────────────────────────────┴────────────────────────────────┘
Replace time zone for an expression of type Datetime.
Time zone for the Datetime expression. Pass null to unset time zone.
Optionalambiguous: string | pl.ExprDetermine how to deal with ambiguous datetimes:
- 'raise' (default): raise
- 'earliest': use the earliest datetime
- 'latest': use the latest datetime
- 'null': set to null
OptionalnonExistent: stringDetermine how to deal with non-existent datetimes:
- 'raise' (default): raise
- 'null': set to null
DataFrame([
Series("london_timezone", [new Date(Date.UTC(2026, 0, 1, 6, 0, 0))],DataType.Datetime("us")).dt.replaceTimeZone("Europe/London")
]).select([ pl.col("london_timezone"),
pl.col("london_timezone").dt.replaceTimeZone("Europe/Amsterdam").alias("London_to_Amsterdam")]);
shape: (1, 2)
┌─────────────────────────────┬────────────────────────────────┐
│ london_timezone ┆ London_to_Amsterdam │
│ --- ┆ --- │
│ datetime[μs, Europe/London] ┆ datetime[μs, Europe/Amsterdam] │
╞═════════════════════════════╪════════════════════════════════╡
│ 2026-01-01 06:00:00 GMT ┆ 2026-01-01 06:00:00 CET │
└─────────────────────────────┴────────────────────────────────┘
You can use `ambiguous` to deal with ambiguous datetimes:
>>> const dates = [
... "2018-10-28 01:30",
... "2018-10-28 02:00",
... "2018-10-28 02:30",
... "2018-10-28 02:00",
... ];
pl.DataFrame(
{
"ts": pl.Series(dates).str.strptime(pl.Datetime),
"ambiguous": ["earliest", "earliest", "latest", "latest"],
}).withColumns(
pl.col("ts").dt.replaceTimeZone( "Europe/Brussels", pl.col("ambiguous")).alias("ts_localized")
);
shape: (4, 3)
┌─────────────────────┬───────────┬───────────────────────────────┐
│ ts ┆ ambiguous ┆ ts_localized │
│ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ str ┆ datetime[μs, Europe/Brussels] │
╞═════════════════════╪═══════════╪═══════════════════════════════╡
│ 2018-10-28 01:30:00 ┆ earliest ┆ 2018-10-28 01:30:00 CEST │
│ 2018-10-28 02:00:00 ┆ earliest ┆ 2018-10-28 02:00:00 CEST │
│ 2018-10-28 02:30:00 ┆ latest ┆ 2018-10-28 02:30:00 CET │
│ 2018-10-28 02:00:00 ┆ latest ┆ 2018-10-28 02:00:00 CET │
Divide the date/datetime range into buckets.
Ambiguous results are localised using the DST offset of the original timestamp -
for example, rounding '2022-11-06 01:20:00 CST' by '1h' results in
'2022-11-06 01:00:00 CST', whereas rounding '2022-11-06 01:20:00 CDT' by
'1h' results in '2022-11-06 01:00:00 CDT'.
Expr Expression of data type :class:Date or :class:Datetime.
The every argument is created with the following small string formatting language:
By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".
--------
const df = pl.DataFrame([
pl.Series("datetime", [
new Date(Date.parse("2020-01-01T01:30:00.002+00:00")),
new Date(Date.parse("2020-01-01T02:02:01.030+00:00")),
new Date(Date.parse("2020-01-01T04:42:20.001+00:00")),
])]);
>>> df.select("datetime", pl.col("datetime").dt.round("1h").alias("hr0"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr0 │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:30:00.002 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 05:00:00 │
└─────────────────────────┴─────────────────────┘
>>> df.select("datetime", pl.col("datetime").dt.round("30m").alias("hr30m"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr30m │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:32:00.002 ┆ 2020-01-01 01:30:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 05:00:00 │
└─────────────────────────┴─────────────────────┘
Format Date/datetime with a formatting rule: See chrono strftime/strptime.
Divide the date/datetime range into buckets. Each date/datetime is mapped to the start of its bucket using the corresponding local datetime. Note that:
'2022-11-06 01:30:00 CST' by
'1h' results in '2022-11-06 01:00:00 CST', whereas truncating
'2022-11-06 01:30:00 CDT' by '1h' results in
'2022-11-06 01:00:00 CDT'.The size of each bucket.
The every argument is created with
the following string language:
By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".
Expr Expression of data type :class:Date or :class:Datetime.
--------
const df = pl.DataFrame([
pl.Series("datetime", [
new Date(Date.parse("2020-01-01T01:32:00.002+00:00")),
new Date(Date.parse("2020-01-01T02:02:01.030+00:00")),
new Date(Date.parse("2020-01-01T04:42:20.001+00:00")),
])]);
>>> df.select("datetime", pl.col("datetime").dt.truncate("1h").alias("hr0"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr0 │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:32:00.002 ┆ 2020-01-01 01:00:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 04:00:00 │
└─────────────────────────┴─────────────────────┘
>>> df.select("datetime", pl.col("datetime").dt.truncate("30m").alias("hr30m"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr30m │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:32:00.002 ┆ 2020-01-01 01:30:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 04:30:00 │
└─────────────────────────┴─────────────────────┘
DateTime functions for expression