Data Analysis / Advanced: Defensible and Reproducible Decisions / Lesson 14
Time Series, Windows, and Leakage
Preserve temporal meaning in aggregation, smoothing, and evaluation instead of treating ordered records as interchangeable rows.
On this page
Lesson goals
- Distinguish event time, ingestion time, and observation windows.
- Use explicit time zones and causal lagged features.
- Evaluate future-facing claims without future information.
A timestamp needs a role and a zone
A time field can mean event occurrence, arrival, processing, or last update. Choose the field matching the question and define whether late records revise earlier periods. Store enough information to distinguish an instant from an ambiguous local civil time. An offset identifies a particular instant, while a named region can carry daylight-saving rules across dates.
Time-zone conversion can move an event to another calendar day. The first program groups two offset-aware timestamps by UTC date. It deliberately uses explicit offsets and UTC rather than relying on the machineโs local zone. A local-business-day report could be valid too, but would answer a different declared question.
from collections import Counter
from datetime import datetime, timezone
tokens = ["2026-10-25T00:30:00+02:00", "2026-10-25T01:00:00+00:00"]
counts = Counter(datetime.fromisoformat(token).astimezone(timezone.utc).date().isoformat() for token in tokens)
for day in sorted(counts):
print(day, counts[day])
2026-10-24 1
2026-10-25 1
The first event is still October24 in UTC; the second belongs to October25.
A missing period is not automatically zero activity
Choose a regular calendar or event-time representation deliberately. A missing day can indicate no events, a capture outage, or an absent extraction partition. Filling every gap with zero asserts an observation process that may be false. Report coverage and exposure so a lower total is not confused with a shorter or incomplete period.
Specify window boundaries, such as start-inclusive and end-exclusive, to avoid double counting adjacent periods. Calendar months have unequal duration, and daylight-saving local days can differ in elapsed hours. Compare rates per appropriate exposure when total opportunities change. Sorting by a display string is unsafe unless its format has the required chronological ordering.
Only past values belong in a past-only feature
The second program computes a two-period trailing mean available before each target observation. The first two targets lack enough history and remain missing. A centered moving average would use later values and is therefore unsuitable as a predictor available at that time, even if it looks smoother in a retrospective chart.
Window alignment, minimum periods, and inclusion of the current observation must be stated. A rolling statistic can be a descriptive smoothing operation or a forecasting feature; the same arithmetic can have different leakage implications. Keep target time and feature-availability time separate.
values = [10, 20, 30, 40]
prior_two_mean = [None, None]
for index in range(2, len(values)):
prior_two_mean.append(sum(values[index-2:index]) / 2)
print(prior_two_mean)
[None, None, 15.0, 25.0]
The third target sees10 and20 only; the fourth sees20 and30, producing15.0 and25.0.
Respect dependence when estimating uncertainty
Time series can contain trend, seasonality, autocorrelation, and regime changes. Consecutive rows may not supply independent information. Applying an independent-row standard-error formula or shuffling all times into random train/test sets can overstate precision or leak future structure. Choose methods and evaluation splits that match the data-generating and deployment process.
For a future-facing model, use a chronological holdout or rolling-origin evaluation with preprocessing learned only from the training period. A seasonal-naive or last-value baseline can reveal whether complexity adds value. The course demonstrates principles, not a claim that one forecasting model is universally appropriate.
Report revisions, horizons, and timing honestly
An analysis can change when late events arrive or historical values are revised. Preserve extraction time and version, distinguish provisional from finalized periods, and avoid comparing a complete past month with a partial current month as if both had equal exposure. State the forecast horizon and when each input was available.
Test events exactly on window boundaries, offset conversions, duplicated timestamps, missing periods, and out-of-order input. Compare totals before and after resampling and account for every record. A smooth line can hide gaps, revisions, or future leakage, so pair it with coverage and a clear method note.
Common errors and fixes
A current-period total looks unusually low.
- Why it happens
- The period is incomplete or capture coverage differs.
- How to fix it
- Compare equal exposure and label provisional periods.
- Prevent it next time
- Track extraction and completion times.
A prediction uses a centered rolling mean.
- Why it happens
- Future observations enter a supposedly available feature.
- How to fix it
- Use explicitly lagged trailing features.
- Prevent it next time
- Audit feature availability at each target time.
Randomly shuffled evaluation looks excellent.
- Why it happens
- Temporal dependence or preprocessing leaks future information.
- How to fix it
- Use time-aware splits and training-only transformations.
- Prevent it next time
- Compare with simple chronological baselines.
Check your understanding
1. Why can two records labeled October25 locally belong to different UTC dates?
Show worked solution
Their offsets and clock times can identify instants on opposite sides of a UTC midnight. Convert aware timestamps before deriving the reporting date, using the zone specified by the metric. Do not strip the offset and group the remaining text. If the question is local business-day activity, use the declared business zone instead of silently switching to UTC.
2. A rolling feature at day10 uses days9,10,11. Is it valid for a forecast made before day10 begins?
Show worked solution
No. It includes the target day and a later day not yet observed. Use only information available before the forecast origin, such as days8 and9 under a two-day history contract. State how early days and missing history are handled. A retrospective smoothing chart may use a centered window, but it must not be mislabeled as a deployable predictor.
Key takeaways
- Time zones, coverage, and window boundaries define the metric.
- Smoothing and prediction have different information-availability contracts.
- Dependence and revisions belong in evaluation and uncertainty.