polars replace-time-zone
for dataframe
Replace the timezone information in a datetime column.
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.
Signature
> polars replace-time-zone {flags} (time_zone)
Flags
--ambiguous, -a {one_of(string, nothing)}
: Determine how to deal with ambiguous datetimes:raise
(default): raise errorearliest
: use the earliest datetimelatest
: use the latest datetimenull
: set to null--nonexistent, -n {one_of(string, nothing)}
: Determine how to deal with non-existent datetimes: raise (default) or null.
Parameters
time_zone
: Timezone for the Datetime Series. Passnull
to unset time zone.
Input/output types:
input | output |
---|---|
any | any |
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 │ 3 years ago │
│ 1 │ 3 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 │ in 6 months │
│ 1 │ │
│ 2 │ in 6 months │
│ 3 │ in 6 months │
╰───┴─────────────╯
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 months ago │
│ 1 │ │
│ 2 │ 2 months ago │
│ 3 │ 2 months ago │
╰───┴──────────────╯