Sub topic 10
Building a Training Set with `get_historical_features`
Assume you know Python, pandas DataFrames, basic machine learning terms, and the difference between batch data and online serving. This chapter focuses on retrieving historical feature values from a Feast feature repository.
A training example represents an entity at a particular time. For example, a driver may have a conversion rate of 0.12 at noon on January 1. A training set must use the value that was available at that time, not a value calculated later. Feast performs this historical lookup with get_historical_features.
Why point-in-time correctness matters
A point-in-time-correct join matches each entity row with feature values available at that row's event_timestamp.
This prevents label leakage. Label leakage occurs when training data contains information that would not have been available when the prediction was made. Leakage can make evaluation look excellent while causing poor production results.
For each entity row, Feast:
- Reads the entity key and event timestamp.
- Finds feature rows for the same entity.
- Selects the newest feature value whose timestamp is at or before the entity timestamp.
- Applies the feature view's schema and time-to-live rules.
- Returns the joined training DataFrame.
Create the entity DataFrame
The entity DataFrame contains the rows for which you need features. It must include every join key used by the requested feature views and an event_timestamp column.
from datetime import datetime
import pandas as pd
from feast import FeatureStore
store = FeatureStore(repo_path=".")
entity_df = pd.DataFrame(
{
"driver_id": [1001, 1002],
"event_timestamp": [
datetime(2025, 1, 1, 12, 0),
datetime(2025, 1, 1, 13, 0),
],
}
)
training_df = store.get_historical_features(
entity_df=entity_df,
features=[
"driver_hourly_stats:conv_rate",
"driver_hourly_stats:acc_rate",
"driver_hourly_stats:avg_daily_trips",
],
).to_df()
print(training_df)The feature references use the form feature_view:feature_name. The returned DataFrame contains the original entity columns, the requested feature columns, and usually timestamp metadata from the source.
Check the time boundary
Suppose the source contains these values:
| Feature timestamp | `conv_rate` | Used for a row at 12:00? |
|---|---|---|
| 11:00 | 0.10 | Yes |
| 12:30 | 0.25 | No |
The 12:30 value is from the future relative to the training example. Feast must not use it.
Use timestamps consistently. Mixing timezone-aware and timezone-naive timestamps can cause errors or unexpected joins. In production, store timestamps in UTC and make the convention explicit in the data pipeline.
A feature view can also define a time-to-live, or TTL. A TTL limits how old a feature value may be. If the newest eligible value is older than the TTL, Feast treats it as unavailable rather than silently using stale data.
Common retrieval errors
- Omitting a join key prevents Feast from identifying the entity.
- Using a feature name that is not registered causes a lookup error.
- Using the current feature table directly can introduce future values.
- Ignoring source timestamps can produce incorrect ordering.
- Treating missing values as fresh values hides data-quality problems.
After retrieval, inspect row counts, null values, timestamp ranges, and feature distributions before training the model.
Exercise
A driver has a feature value at 09:00 and another at 11:00. Which value should Feast use for an entity row timestamped 10:00?
Show answer
Feast should use the 09:00 value because it is the newest value available at or before 10:00. The 11:00 value must be excluded as future information.