Create time-based features without looking into the future
In this article (3 sections)
A rolling count is correct only relative to a decision timestamp. If the aggregation includes events recorded later, the model learns evidence that would not exist when the prediction is made.
Write the interval before the query
Our contract defines prior_7d_events on the half-open interval [decision_day - 7, decision_day). Events exactly at decision time are excluded. Customer A has events on days 1, 4 and 9; B on days 2 and 8.
For A at day 5, the correct count is two. For A at day 10, days 4 and 9 count, again two. For B at day 7, only day 2 counts, giving one.
An intentionally leaky calculation includes all events through two days after the decision. It returns two, three and two respectively. The day-9 A event leaks into the day-7-style future window, and B's day-8 event leaks into the decision at day 7.
from feature_cases import time_feature_case
r = time_feature_case()
assert [row['prior_7d_events'] for row in r['features']] == [2, 2, 1]
assert [row['leaky_events_through_plus_2d'] for row in r['features']] == [2, 3, 2]
print(r)Run the exact event join in the feature-engineering lab. Its small size makes boundary conditions visible.
Distinguish event time from availability time
An event can occur before decision time but arrive in the warehouse later. For historical training, use the version that would have been available, or account explicitly for ingestion delay. Event time alone is insufficient for point-in-time correctness.
Define time zone, window start and end inclusivity, late-arrival policy, cancellations and duplicate events. Use stable entity keys and prevent joins that multiply events. Test boundary timestamps such as exactly seven days earlier and exactly at decision time.
Backfills are dangerous because a current table may contain corrections unavailable historically. Snapshot data, temporal tables or feature-store point-in-time joins can reconstruct as-of values. Validate with mutation tests: add a future event and confirm past features do not change.
Exercise: add event-availability timestamps and a two-day ingestion delay. Recompute features as-of the decision, then backfill a corrected event. Write tests for event-time, availability-time and half-open boundaries.
NeuraPath's Data Science course teaches time features as reproducible as-of queries. The feature definition includes its clock and availability contract.
Continue learning
This article is part of the Feature engineering and data quality sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Imputation strategies under different missingness patterns.
- Continue with Feature selection nested inside validation.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science