Skip to content

Lesson 6 of 9 · 26m

Time Series Without the Timezone Hangover

Datetime parsing, timezone policy, resampling semantics, ordering, deduplication, and the edge cases that corrupt metrics.

Time series work is where pandas is genuinely excellent — and where slightly careless data pipelines produce daily metrics that are technically reproducible and substantively wrong. The hard part is not resampling syntax. It is deciding what a timestamp means before arithmetic starts.

Parse once, at the boundary

df["created_at"] = pd.to_datetime(df["created_at"], utc=True, errors="raise")

utc=True is not a preference; it is a normalization contract. If inputs mix offsets (2026-01-01T10:00:00-05:00) and naive strings, pandas otherwise falls back to object datetimes or inconsistent inference. Store internally in UTC. Convert to a display timezone only at presentation edges:

local = df["created_at"].dt.tz_convert("America/New_York")

A timezone-aware instant answers "when did this happen?" A naive local wall time answers "what did the clock say?" They are different questions and bad things happen when you confuse them.

Sorting and deduplication come before resampling

df = df.sort_values("created_at")
df = df.drop_duplicates(subset=["event_id"], keep="first")

Out-of-order events break rolling calculations quietly. Duplicate event delivery is normal in real systems — retries, consumers, queue replays — so event_id deduplication is a correctness step, not hygiene. Decide whether first or last wins and write it down.

resample is groupby over time bins

daily = (
    df.set_index("created_at")
      .resample("1D")["amount"]
      .sum()
)

1D means UTC calendar bins when the index is UTC-aware. Need business-local days? Convert to the business timezone first, then resample, because day boundaries move. Also know whether empty intervals should be zero or missing:

daily = daily.asfreq("1D")          # preserves missing
filled = daily.fillna(0)             # explicit zero activity

Zero and missing are not interchangeable. Zero means "we observed the system and nothing happened." Missing means "we do not know."

Rolling windows magnify grain mistakes

df = df.set_index("created_at").sort_index()
rolling = df["amount"].rolling("7D").sum()

Time-based rolling windows depend on the index and include uneven numbers of rows. Count-based rolling (rolling(7)) assumes regular observations. Pick the one that matches the data's cadence, not the one that happens to produce a smoother chart.

Field notes

  • Store UTC internally; convert to local time only where humans consume output.
  • dt.tz_localize is for naive stamps known to be in a zone; dt.tz_convert is for aware stamps.
  • Daylight-saving transitions make local midnight arithmetic genuinely tricky.
  • Deduplicate events before aggregates; late retries are normal in streaming systems.
  • Missing intervals and zero-activity intervals must stay distinct unless the business says otherwise.

Was this lesson helpful?

Sign in to rate