Sub topic 4
Storing Feature History Instead of Only the Latest Value
Prerequisites and the core idea
Assume you know basic supervised machine learning, pandas DataFrames, SQL joins, and timestamps with time zones.
A feature is an input value used by a model. A feature store or database often keeps only the latest value for each entity, such as a customer's current average spend. That design is convenient for serving predictions now, but it is unsafe for building historical training data.
A training row represents a prediction made at a particular time. The row must use the feature value that was available at that time, not the value calculated today. Therefore, feature data needs history: multiple records for the same entity, each with a timestamp.
A history table
An append-only history table adds a new record when a feature changes. It does not overwrite the old record.
| Column | Meaning |
|---|---|
| `customer_id` | Entity to which the feature belongs |
| `feature_name` | Name of the feature |
| `feature_value` | Value calculated for the entity |
| `event_ts` | Time represented by the underlying business events |
| `ingested_ts` | Time the feature record entered the data system |
| `feature_version` | Logic or code version that produced the value |
For example, the avg_spend_30d value for customer 42 might change every day. A historical table preserves each value:
- At January 1, the value was 80.
- At January 10, the value was 95.
- At January 20, the value was 70.
If you replace all three records with 70, a training example from January 5 will incorrectly see information from January 20.
Selecting a historical value
The following example keeps feature history and selects the latest record whose event time is no later than the prediction time.
import pandas as pd
history = pd.DataFrame({
"customer_id": [42, 42, 42],
"event_ts": pd.to_datetime(["2026-01-01", "2026-01-10", "2026-01-20"]),
"ingested_ts": pd.to_datetime(["2026-01-01", "2026-01-10", "2026-01-20"]),
"avg_spend_30d": [80.0, 95.0, 70.0],
})
prediction_ts = pd.Timestamp("2026-01-12")
available = history[
(history["event_ts"] <= prediction_ts)
& (history["ingested_ts"] <= prediction_ts)
]
feature_at_prediction = (
available.sort_values("event_ts")
.iloc[-1]
)
print(feature_at_prediction["avg_spend_30d"]) # 95.0The prediction on January 12 receives 95.0. A table containing only the current value would return 70.0 and leak future information.
The event_ts condition answers, “When did this feature describe the business state?” The ingested_ts condition answers, “When could the training pipeline have known about it?” Keeping both timestamps supports late-arriving data and corrected records.
Common storage errors
- Overwriting rows: An update statement destroys the values needed for older predictions.
- Using processing time as event time: A feature may arrive late, so arrival time does not always describe when the underlying activity happened.
- Missing version information: Recomputing a feature with changed logic makes old training sets difficult to reproduce.
- Unclear entity keys: A feature keyed only by customer may accidentally mix regions, accounts, or product scopes.
- Unbounded history: Retain enough history for the model's training period and audit needs, then apply an explicit retention policy.
History storage is the foundation for a point-in-time join. The join can then choose one record per entity and prediction timestamp without consulting future rows.
Exercise
Why should a feature table preserve both the old value and the new value when a feature changes?
Show answer
Older training examples need the value that was available at their prediction time. Overwriting the old value would replace historical knowledge with a value that may have been computed later.