β²οΈ Convert to local time zone#
Demonstrates the following:
Converting UTC to stationβs local time
SynotpicPy always parses date-time information as UTC. But you can convert it to the stations local timezone.
[1]:
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β¦
[2]:
df = synoptic.TimeSeries(
stid="wbb",
start=datetime(2024, 1, 1),
end=datetime(2024, 1, 5),
obtimezone="local",
vars=["air_temp"],
).df()
df.head()
/home/blaylock/GITHUB/SynopticPy/src/synoptic/params.py:168: UserWarning: The 'obtimezone' key is ignored by SynopticPy.
warnings.warn(f"The '{key}' key is ignored by SynopticPy.", UserWarning)
ππ¨ Speedy delivery from Synoptic's timeseries service.
π¦ Received data from 1 stations (0.38 seconds).
[2]:
shape: (5, 21)
| stid | date_time | variable | sensor_index | is_derived | value | units | id | name | elevation | latitude | longitude | mnet_id | state | timezone | elev_dem | period_of_record_start | period_of_record_end | qc_flagged | is_restricted | is_active |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | datetime[ΞΌs, UTC] | str | u32 | bool | f64 | str | u32 | str | f64 | f64 | f64 | u32 | str | str | f64 | datetime[ΞΌs, UTC] | datetime[ΞΌs, UTC] | bool | bool | bool |
| "WBB" | 2024-01-01 00:00:00 UTC | "air_temp" | 1 | false | 4.194 | "Celsius" | 1 | "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-12-03 05:10:00 UTC | false | false | true |
| "WBB" | 2024-01-01 00:01:00 UTC | "air_temp" | 1 | false | 4.083 | "Celsius" | 1 | "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-12-03 05:10:00 UTC | false | false | true |
| "WBB" | 2024-01-01 00:02:00 UTC | "air_temp" | 1 | false | 3.983 | "Celsius" | 1 | "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-12-03 05:10:00 UTC | false | false | true |
| "WBB" | 2024-01-01 00:03:00 UTC | "air_temp" | 1 | false | 3.928 | "Celsius" | 1 | "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-12-03 05:10:00 UTC | false | false | true |
| "WBB" | 2024-01-01 00:04:00 UTC | "air_temp" | 1 | false | 3.894 | "Celsius" | 1 | "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-12-03 05:10:00 UTC | false | false | true |
[3]:
# 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()
[3]:
shape: (5, 21)
| stid | date_time | variable | sensor_index | is_derived | value | units | id | name | elevation | latitude | longitude | mnet_id | state | timezone | elev_dem | period_of_record_start | period_of_record_end | qc_flagged | is_restricted | is_active |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | datetime[ΞΌs, America/Denver] | str | u32 | bool | f64 | str | u32 | str | f64 | f64 | f64 | u32 | str | str | f64 | datetime[ΞΌs, America/Denver] | datetime[ΞΌs, America/Denver] | bool | bool | bool |
| "WBB" | 2023-12-31 17:00:00 MST | "air_temp" | 1 | false | 4.194 | "Celsius" | 1 | "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-12-02 22:10:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:01:00 MST | "air_temp" | 1 | false | 4.083 | "Celsius" | 1 | "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-12-02 22:10:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:02:00 MST | "air_temp" | 1 | false | 3.983 | "Celsius" | 1 | "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-12-02 22:10:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:03:00 MST | "air_temp" | 1 | false | 3.928 | "Celsius" | 1 | "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-12-02 22:10:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:04:00 MST | "air_temp" | 1 | false | 3.894 | "Celsius" | 1 | "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-12-02 22:10:00 MST | false | 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.
[4]:
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's timeseries service.
π¦ Received data from 2 stations (0.21 seconds).
[4]:
{'America/Denver': shape: (1_292, 21)
ββββββββ¬βββββββββββββ¬βββββββββββ¬βββββββββββββ¬ββββ¬βββββββββββββ¬βββββββββββββ¬βββββββββββββ¬ββββββββββββ
β stid β date_time β variable β sensor_ind β β¦ β period_of_ β qc_flagged β is_restric β is_active β
β --- β --- β --- β ex β β record_end β --- β ted β --- β
β str β datetime[ΞΌ β str β --- β β --- β bool β --- β bool β
β β s, America β β u32 β β datetime[ΞΌ β β bool β β
β β /Denver] β β β β s, America β β β β
β β β β β β /Denver] β β β β
ββββββββͺβββββββββββββͺβββββββββββͺβββββββββββββͺββββͺβββββββββββββͺβββββββββββββͺβββββββββββββͺββββββββββββ‘
β KSLC β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 17:00:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 17:05:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 17:10:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 17:15:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 17:20:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β
β KSLC β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:45:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:50:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:54:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:55:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
β KSLC β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 17:00:00 β β β β 22:00:00 β β β β
β β MST β β β β MST β β β β
ββββββββ΄βββββββββββββ΄βββββββββββ΄βββββββββββββ΄ββββ΄βββββββββββββ΄βββββββββββββ΄βββββββββββββ΄ββββββββββββ,
'America/Los_Angeles': shape: (1_219, 21)
ββββββββ¬βββββββββββββ¬βββββββββββ¬βββββββββββββ¬ββββ¬βββββββββββββ¬βββββββββββββ¬βββββββββββββ¬ββββββββββββ
β stid β date_time β variable β sensor_ind β β¦ β period_of_ β qc_flagged β is_restric β is_active β
β --- β --- β --- β ex β β record_end β --- β ted β --- β
β str β datetime[ΞΌ β str β --- β β --- β bool β --- β bool β
β β s, America β β u32 β β datetime[ΞΌ β β bool β β
β β /Los_Angel β β β β s, America β β β β
β β es] β β β β /Los_Angel β β β β
β β β β β β es] β β β β
ββββββββͺβββββββββββββͺβββββββββββͺβββββββββββββͺββββͺβββββββββββββͺβββββββββββββͺβββββββββββββͺββββββββββββ‘
β KMRY β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:00:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:05:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:10:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:15:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2023-12-31 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:20:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β
β KMRY β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 15:40:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 15:45:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 15:54:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 15:55:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
β KMRY β 2024-01-04 β air_temp β 1 β β¦ β 2024-12-02 β false β false β true β
β β 16:00:00 β β β β 21:00:00 β β β β
β β PST β β β β PST β β β β
ββββββββ΄βββββββββββββ΄βββββββββββ΄βββββββββββββ΄ββββ΄βββββββββββββ΄βββββββββββββ΄βββββββββββββ΄ββββββββββββ}
Synoptic Namespace#
The same is implemented in the synoptic namespace; If there is one timezone, then return a DataFrame, if more than one timezone then return a dictionary of DataFrames.
[5]:
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's timeseries service.
π¦ Received data from 1 stations (0.30 seconds).
[5]:
shape: (5_761, 21)
| stid | date_time | variable | sensor_index | is_derived | value | units | id | name | elevation | latitude | longitude | mnet_id | state | timezone | elev_dem | period_of_record_start | period_of_record_end | qc_flagged | is_restricted | is_active |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | datetime[ΞΌs, America/Denver] | str | u32 | bool | f64 | str | u32 | str | f64 | f64 | f64 | u32 | str | str | f64 | datetime[ΞΌs, America/Denver] | datetime[ΞΌs, America/Denver] | bool | bool | bool |
| "WBB" | 2023-12-31 17:00:00 MST | "air_temp" | 1 | false | 4.194 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:01:00 MST | "air_temp" | 1 | false | 4.083 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:02:00 MST | "air_temp" | 1 | false | 3.983 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:03:00 MST | "air_temp" | 1 | false | 3.928 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2023-12-31 17:04:00 MST | "air_temp" | 1 | false | 3.894 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … |
| "WBB" | 2024-01-04 16:56:00 MST | "air_temp" | 1 | false | -0.033 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2024-01-04 16:57:00 MST | "air_temp" | 1 | false | -0.094 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2024-01-04 16:58:00 MST | "air_temp" | 1 | false | -0.106 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2024-01-04 16:59:00 MST | "air_temp" | 1 | false | -0.072 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |
| "WBB" | 2024-01-04 17:00:00 MST | "air_temp" | 1 | false | -0.067 | "Celsius" | 1 | "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-12-02 22:05:00 MST | false | false | true |