β²οΈ Convert to local time zone#
SynotpicPy always parses date-time information as UTC. But you can convert it to the stations local timezone.
[2]:
from datetime import datetime
import polars as pl
import polars.selectors as cs
import synoptic
Single time zone#
If there is only one unique timezone, this conversion is straightforwardβ¦
[3]:
df = synoptic.TimeSeries(
stid="wbb",
start=datetime(2024, 1, 1),
end=datetime(2024, 1, 5),
obtimezone="local",
vars=["air_temp"],
).df()
df.head()
ππ¨ Speedy delivery from Synoptic timeseries service.
π¦ Received data from 1 stations.
[3]:
shape: (5, 20)
| date_time | variable | sensor_index | is_derived | value | units | id | stid | name | elevation | latitude | longitude | mnet_id | state | timezone | elev_dem | period_of_record_start | period_of_record_end | is_restricted | is_active |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| datetime[ΞΌs, UTC] | str | u32 | bool | f64 | str | u32 | str | str | f64 | f64 | f64 | u32 | str | str | f64 | datetime[ΞΌs, UTC] | datetime[ΞΌs, UTC] | bool | bool |
| 2024-01-01 00:00:00 UTC | "air_temp" | 1 | false | 4.194 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1997-01-01 00:00:00 UTC | 2024-11-10 01:20:00 UTC | false | true |
| 2024-01-01 00:01:00 UTC | "air_temp" | 1 | false | 4.083 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1997-01-01 00:00:00 UTC | 2024-11-10 01:20:00 UTC | false | true |
| 2024-01-01 00:02:00 UTC | "air_temp" | 1 | false | 3.983 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1997-01-01 00:00:00 UTC | 2024-11-10 01:20:00 UTC | false | true |
| 2024-01-01 00:03:00 UTC | "air_temp" | 1 | false | 3.928 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1997-01-01 00:00:00 UTC | 2024-11-10 01:20:00 UTC | false | true |
| 2024-01-01 00:04:00 UTC | "air_temp" | 1 | false | 3.894 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1997-01-01 00:00:00 UTC | 2024-11-10 01:20:00 UTC | false | true |
[4]:
# Get the unique timezone value
tz = df["timezone"].unique().item()
# Convert all datetime columns to the time zone.
df.with_columns(cs.datetime().dt.convert_time_zone(tz)).head()
[4]:
shape: (5, 20)
| date_time | variable | sensor_index | is_derived | value | units | id | stid | name | elevation | latitude | longitude | mnet_id | state | timezone | elev_dem | period_of_record_start | period_of_record_end | is_restricted | is_active |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| datetime[ΞΌs, America/Denver] | str | u32 | bool | f64 | str | u32 | str | str | f64 | f64 | f64 | u32 | str | str | f64 | datetime[ΞΌs, America/Denver] | datetime[ΞΌs, America/Denver] | bool | bool |
| 2023-12-31 17:00:00 MST | "air_temp" | 1 | false | 4.194 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:01:00 MST | "air_temp" | 1 | false | 4.083 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:02:00 MST | "air_temp" | 1 | false | 3.983 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:03:00 MST | "air_temp" | 1 | false | 3.928 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:04:00 MST | "air_temp" | 1 | false | 3.894 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
Multiple time zones#
If your stations span mulitiple time zones, youβll have to partition the DataFrame by timezone. Notice that this produces a list of DataFrames with a DataFrame for each timezone.
[ ]:
df = synoptic.TimeSeries(
stid="kslc,kmry",
start=datetime(2024, 1, 1),
end=datetime(2024, 1, 5),
obtimezone="local",
vars=["air_temp"],
).df()
df_tz = {}
for i in df.partition_by("timezone"):
tz = i["timezone"].unique().item()
df_tz[tz] = i.with_columns(cs.datetime().dt.convert_time_zone(tz))
df_tz
ππ¨ Speedy delivery from Synoptic timeseries service.
π¦ Received data from 2 stations.
{'America/Denver': shape: (1_292, 20)
βββββββββββββ¬βββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββ
β date_time β variable β sensor_in β is_derive β β¦ β period_of β period_of β is_restri β is_active β
β --- β --- β dex β d β β _record_s β _record_e β cted β --- β
β datetime[ β str β --- β --- β β tart β nd β --- β bool β
β ΞΌs, Ameri β β u32 β bool β β --- β --- β bool β β
β ca/Denver β β β β β datetime[ β datetime[ β β β
β ] β β β β β ΞΌs, Ameri β ΞΌs, Ameri β β β
β β β β β β ca/Denver β ca/Denver β β β
β β β β β β ] β ] β β β
βββββββββββββͺβββββββββββͺββββββββββββͺββββββββββββͺββββͺββββββββββββͺββββββββββββͺββββββββββββͺββββββββββββ‘
β 2023-12-3 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 17:00:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 17:05:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 17:10:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 17:15:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 17:20:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 16:45:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 16:50:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 16:54:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 16:55:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1996-12-3 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 17:00:00 β β β β β 17:00:00 β 18:20:00 β β β
β MST β β β β β MST β MST β β β
βββββββββββββ΄βββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββ,
'America/Los_Angeles': shape: (1_219, 20)
βββββββββββββ¬βββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββ
β date_time β variable β sensor_in β is_derive β β¦ β period_of β period_of β is_restri β is_active β
β --- β --- β dex β d β β _record_s β _record_e β cted β --- β
β datetime[ β str β --- β --- β β tart β nd β --- β bool β
β ΞΌs, Ameri β β u32 β bool β β --- β --- β bool β β
β ca/Los_An β β β β β datetime[ β datetime[ β β β
β geles] β β β β β ΞΌs, Ameri β ΞΌs, Ameri β β β
β β β β β β ca/Los_An β ca/Los_An β β β
β β β β β β geles] β geles] β β β
βββββββββββββͺβββββββββββͺββββββββββββͺββββββββββββͺββββͺββββββββββββͺββββββββββββͺββββββββββββͺββββββββββββ‘
β 2023-12-3 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 16:00:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 16:05:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 16:10:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 16:15:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2023-12-3 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 1 β β β β β 1 β 9 β β β
β 16:20:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 15:40:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 15:45:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 15:54:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 15:55:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
β 2024-01-0 β air_temp β 1 β false β β¦ β 1997-04-1 β 2024-11-0 β false β true β
β 4 β β β β β 1 β 9 β β β
β 16:00:00 β β β β β 17:00:00 β 17:20:00 β β β
β PST β β β β β PDT β PST β β β
βββββββββββββ΄βββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββ}
Synoptic Namespace#
The same is implemented in the synoptic namespace
[6]:
df = synoptic.TimeSeries(
stid="wbb",
start=datetime(2024, 1, 1),
end=datetime(2024, 1, 5),
obtimezone="local",
vars=["air_temp"],
).df()
df.synoptic.with_local_timezone()
ππ¨ Speedy delivery from Synoptic timeseries service.
π¦ Received data from 1 stations.
[6]:
shape: (5_761, 20)
| date_time | variable | sensor_index | is_derived | value | units | id | stid | name | elevation | latitude | longitude | mnet_id | state | timezone | elev_dem | period_of_record_start | period_of_record_end | is_restricted | is_active |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| datetime[ΞΌs, America/Denver] | str | u32 | bool | f64 | str | u32 | str | str | f64 | f64 | f64 | u32 | str | str | f64 | datetime[ΞΌs, America/Denver] | datetime[ΞΌs, America/Denver] | bool | bool |
| 2023-12-31 17:00:00 MST | "air_temp" | 1 | false | 4.194 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:01:00 MST | "air_temp" | 1 | false | 4.083 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:02:00 MST | "air_temp" | 1 | false | 3.983 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:03:00 MST | "air_temp" | 1 | false | 3.928 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2023-12-31 17:04:00 MST | "air_temp" | 1 | false | 3.894 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … |
| 2024-01-04 16:56:00 MST | "air_temp" | 1 | false | -0.033 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2024-01-04 16:57:00 MST | "air_temp" | 1 | false | -0.094 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2024-01-04 16:58:00 MST | "air_temp" | 1 | false | -0.106 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2024-01-04 16:59:00 MST | "air_temp" | 1 | false | -0.072 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |
| 2024-01-04 17:00:00 MST | "air_temp" | 1 | false | -0.067 | "Celsius" | 1 | "WBB" | "U of U William Browning Buildi⦠| 4806.0 | 40.76623 | -111.84755 | 153 | "UT" | "America/Denver" | 4727.7 | 1996-12-31 17:00:00 MST | 2024-11-09 18:20:00 MST | false | true |