⏲️ 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)
stiddate_timevariablesensor_indexis_derivedvalueunitsidnameelevationlatitudelongitudemnet_idstatetimezoneelev_demperiod_of_record_startperiod_of_record_endqc_flaggedis_restrictedis_active
strdatetime[ΞΌs, UTC]stru32boolf64stru32strf64f64f64u32strstrf64datetime[ΞΌs, UTC]datetime[ΞΌs, UTC]boolboolbool
"WBB"2024-01-01 00:00:00 UTC"air_temp"1false4.194"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71997-01-01 00:00:00 UTC2024-12-03 05:10:00 UTCfalsefalsetrue
"WBB"2024-01-01 00:01:00 UTC"air_temp"1false4.083"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71997-01-01 00:00:00 UTC2024-12-03 05:10:00 UTCfalsefalsetrue
"WBB"2024-01-01 00:02:00 UTC"air_temp"1false3.983"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71997-01-01 00:00:00 UTC2024-12-03 05:10:00 UTCfalsefalsetrue
"WBB"2024-01-01 00:03:00 UTC"air_temp"1false3.928"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71997-01-01 00:00:00 UTC2024-12-03 05:10:00 UTCfalsefalsetrue
"WBB"2024-01-01 00:04:00 UTC"air_temp"1false3.894"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71997-01-01 00:00:00 UTC2024-12-03 05:10:00 UTCfalsefalsetrue
[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)
stiddate_timevariablesensor_indexis_derivedvalueunitsidnameelevationlatitudelongitudemnet_idstatetimezoneelev_demperiod_of_record_startperiod_of_record_endqc_flaggedis_restrictedis_active
strdatetime[ΞΌs, America/Denver]stru32boolf64stru32strf64f64f64u32strstrf64datetime[ΞΌs, America/Denver]datetime[ΞΌs, America/Denver]boolboolbool
"WBB"2023-12-31 17:00:00 MST"air_temp"1false4.194"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:10:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:01:00 MST"air_temp"1false4.083"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:10:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:02:00 MST"air_temp"1false3.983"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:10:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:03:00 MST"air_temp"1false3.928"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:10:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:04:00 MST"air_temp"1false3.894"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:10:00 MSTfalsefalsetrue

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)
stiddate_timevariablesensor_indexis_derivedvalueunitsidnameelevationlatitudelongitudemnet_idstatetimezoneelev_demperiod_of_record_startperiod_of_record_endqc_flaggedis_restrictedis_active
strdatetime[ΞΌs, America/Denver]stru32boolf64stru32strf64f64f64u32strstrf64datetime[ΞΌs, America/Denver]datetime[ΞΌs, America/Denver]boolboolbool
"WBB"2023-12-31 17:00:00 MST"air_temp"1false4.194"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:01:00 MST"air_temp"1false4.083"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:02:00 MST"air_temp"1false3.983"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:03:00 MST"air_temp"1false3.928"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2023-12-31 17:04:00 MST"air_temp"1false3.894"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2024-01-04 16:56:00 MST"air_temp"1false-0.033"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2024-01-04 16:57:00 MST"air_temp"1false-0.094"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2024-01-04 16:58:00 MST"air_temp"1false-0.106"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2024-01-04 16:59:00 MST"air_temp"1false-0.072"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue
"WBB"2024-01-04 17:00:00 MST"air_temp"1false-0.067"Celsius"1"U of U William Browning Buildi…4806.040.76623-111.84755153"UT""America/Denver"4727.71996-12-31 17:00:00 MST2024-12-02 22:05:00 MSTfalsefalsetrue