polars replace-time-zone

For dataframe.

Replace the timezone information in a datetime column.

warning This command requires a plugin The polars replace-time-zone command resides in the polars plugin. To use this command, you must install and register nu_plugin_polars. See the Plugins chapter in the book for more information.

Command Type

plugin

Signature

> polars replace-time-zone {flags} (time_zone)

Flags

  • --ambiguous, -a {oneof<string, nothing>}: Determine how to deal with ambiguous datetimes: raise (default): raise error earliest: use the earliest datetime latest: use the latest datetime null: set to null
  • --nonexistent, -n {oneof<string, nothing>}: Determine how to deal with non-existent datetimes: raise (default) or null.

Parameters

  • time_zone: Timezone for the Datetime Series. Pass null to unset time zone.

Input/output types:

input output
polars_expression polars_expression
polars_selector polars_expression

Examples

Apply timezone to a naive datetime

> ["2021-12-30 00:00:00" "2021-12-31 00:00:00"] | polars into-df
                    | polars as-datetime "%Y-%m-%d %H:%M:%S" --naive
                    | polars select (polars col datetime | polars replace-time-zone "America/New_York")
╭───┬─────────────╮
│ # │  datetime   │
├───┼─────────────┤
│ 0 │ 4 years ago │
│ 1 │ 4 years ago │
╰───┴─────────────╯

Apply timezone with ambiguous datetime

> ["2025-11-02 00:00:00", "2025-11-02 01:00:00", "2025-11-02 02:00:00", "2025-11-02 03:00:00"]
                    | polars into-df
                    | polars as-datetime "%Y-%m-%d %H:%M:%S" --naive
                    | polars select (polars col datetime | polars replace-time-zone "America/New_York" --ambiguous null)
╭───┬───────────────╮
│ # │   datetime    │
├───┼───────────────┤
│ 0 │ 10 months ago │
│ 1 │               │
│ 2 │ 10 months ago │
│ 3 │ 10 months ago │
╰───┴───────────────╯

Apply timezone with nonexistent datetime

> ["2025-03-09 01:00:00", "2025-03-09 02:00:00", "2025-03-09 03:00:00", "2025-03-09 04:00:00"]
                    | polars into-df
                    | polars as-datetime "%Y-%m-%d %H:%M:%S" --naive
                    | polars select (polars col datetime | polars replace-time-zone "America/New_York" --nonexistent null)
╭───┬─────────────╮
│ # │  datetime   │
├───┼─────────────┤
│ 0 │ 2 years ago │
│ 1 │             │
│ 2 │ 2 years ago │
│ 3 │ 2 years ago │
╰───┴─────────────╯

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.