Skip to content

Turn UTC timestamps into local days

Draft: not yet validated

The code on this page was run inside MESA JupyterLab on 2026-11-13 and produced the output shown. Nobody has yet followed the steps there by hand, so a menu or a screen may differ. Tell us what you find.

Instruments usually record time in UTC. A "day" of UTC is not a day where the instrument stands: on the invented campus, which keeps US Mountain Time, the UTC date changes at six in the evening in July. Evening rain lands on the wrong day unless you convert first.

You will: have daily rain totals by local day that match the gauge's daily record.

Time: 10 minutes. You need: the notebook from Load a file from your Data Store into a notebook, open in MESA JupyterLab, with its first cell run.

Steps

  1. Check that observations.csv is in your mesa-practice folder. If it is not, run the download cell on the practice data page again. Then run:

    obs = pd.read_csv("observations.csv")
    obs.shape
    

    You should see:

    (336, 4)
    
  2. Keep the rain rows and turn the text timestamps into real ones:

    rain = obs[obs["measurement"] == "rain_mm"].copy()
    rain["time_utc"] = pd.to_datetime(rain["timestamp_utc"])
    rain["time_utc"].dt.tz
    

    You should see: UTC. The trailing Z in the file told pandas the zone.

  3. Total the rain by UTC date:

    rain.groupby(rain["time_utc"].dt.date)["value"].sum()
    

    You should see: eight dates for a seven-day week, the first sign of trouble.

    time_utc
    2026-07-10     0.0
    2026-07-11    32.4
    2026-07-12     7.4
    2026-07-13    17.6
    2026-07-14     4.4
    2026-07-15    22.1
    2026-07-16     1.7
    2026-07-17     0.0
    Name: value, dtype: float64
    
  4. Convert to local time and total again:

    rain["time_local"] = rain["time_utc"].dt.tz_convert("America/Denver")
    rain.groupby(rain["time_local"].dt.date)["value"].sum()
    

    You should see: seven dates, and different totals.

    time_local
    2026-07-10    32.4
    2026-07-11     0.0
    2026-07-12    23.0
    2026-07-13     6.4
    2026-07-14    22.1
    2026-07-15     1.7
    2026-07-16     0.0
    Name: value, dtype: float64
    
  5. Compare with the same gauge's daily record:

    week = readings["date"].between("2026-07-10", "2026-07-16")
    readings[(readings["gauge_id"] == "G03") & week][["date", "value"]]
    

    You should see: the seven totals from step 4, not those from step 3.

Check it worked

Your local-day totals equal the daily values in readings.csv for G03, day for day. The week's total is 85.6 mm either way; only the days it falls on change.

If it didn't

TypeError: Cannot convert tz-naive timestamps, use tz_localize to localize. The timestamps were read without a zone, which happens when a file's times have no Z or offset. Say what zone they are in first: rain["time_utc"].dt.tz_localize("UTC"), then convert.

Next

Not yet validated by a person in MESA JupyterLab. Drafted with AI assistance on 2026-11-13; the code was run there on 2026-11-13 (pandas 3.1.1, DuckDB 2.0.0).

Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.