Sub topic 9
Point-in-time correct joins and avoiding label leakage
This chapter assumes you can read a pandas DataFrame, know what a feature view and an entity are in Feast, and have seen get_historical_features once. Here you will learn what "point-in-time correct" means, how Feast enforces it, and which mistakes still let future information into a training set.
The problem: features that did not exist yet
A training row pairs a label with the features a model would have seen when it made the prediction. Suppose you predict at noon whether a driver will accept a trip. If the feature conv_rate is joined from the latest value in the table, you may get the value computed at 14:00. The model then learns from information that was not available at noon. This is called label leakage, or more generally data leakage.
Leakage inflates offline metrics. The model looks accurate in validation and then degrades in production, where only past values exist. It is a main cause of training-serving skew.
A point-in-time (PIT) correct join fixes this. For each row in your entity DataFrame, it returns the feature values as they were known at that row's timestamp, and nothing later.
The rule Feast applies
For every row in the entity DataFrame, Feast looks at the feature view's source and applies these steps:
- Keep only source rows with the same join key, such as
driver_id. - Discard source rows whose
event_timestampis later than the entity row'sevent_timestamp. - If the feature view has a TTL, also discard rows older than the entity timestamp minus the TTL.
- Of the rows left, choose the one with the latest
event_timestamp. - If several rows share that timestamp, use the
created_timestamp_columnto choose the most recently created one. - If no row remains, return a null for those features.
The comparison is "at or before", so a feature row stamped exactly at the entity timestamp is allowed.
The same rule in pandas
pandas.merge_asof implements a backward-looking join with the same semantics. It is a handy way to check your understanding without running Feast.
import pandas as pd
features = pd.DataFrame(
{
"driver_id": [1001, 1002, 1001, 1001],
"event_timestamp": pd.to_datetime(
[
"2025-01-01 08:00",
"2025-01-01 09:00",
"2025-01-01 11:00",
"2025-01-01 14:00",
]
),
"conv_rate": [0.50, 0.30, 0.60, 0.90],
}
).sort_values("event_timestamp")
entity_df = pd.DataFrame(
{
"driver_id": [1001, 1002, 1003, 1002],
"event_timestamp": pd.to_datetime(
[
"2025-01-01 12:00",
"2025-01-01 13:00",
"2025-01-01 13:00",
"2025-01-03 13:00",
]
),
}
).sort_values("event_timestamp")
result = pd.merge_asof(
entity_df,
features,
on="event_timestamp",
by="driver_id",
direction="backward",
tolerance=pd.Timedelta(days=1), # plays the role of the TTL
)
print(result)The four rows come out as follows:
- Driver 1001 at 12:00 gets
0.60. The 11:00 value is the latest one at or before noon. The 14:00 value of0.90is in the future and is ignored. - Driver 1002 at 13:00 gets
0.30, from 09:00. - Driver 1003 has no feature rows, so the value is
NaN. - Driver 1002 on January 3 gets
NaN. The only value is more than one day older than the entity timestamp, so the tolerance (the TTL in Feast) rejects it.
Doing it in Feast
The Feast call has the same shape. Your entity DataFrame must contain the join key columns 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()Each output row keeps the entity columns and adds the requested features. The join runs inside the offline store (for the file offline store, in the local process), so the work scales with the offline store you chose.
What the TTL does in a historical join
In a historical join the TTL is a lower bound on how old a value may be. With ttl=timedelta(days=1), a value stamped 30 hours before the entity timestamp is discarded and you get null. This matches serving behavior, where an old value is treated as missing. Training on values the online store would have refused to serve is one way skew appears, so the TTL belongs in your training logic as well as your serving logic.
A TTL of zero means values never expire. That is convenient for slowly changing attributes, but it hides staleness, so choose it deliberately.
Where leakage still gets in
Feast guarantees the join rule. It cannot know whether your timestamps mean what you think they mean. These are the common errors.
Wrong timestamp in the entity DataFrame
The entity timestamp must be the moment the prediction would have been made. A frequent mistake is using the time the label was observed. For example, in a delivery-time model, using the delivery time as event_timestamp joins in every feature computed up to delivery, including ones from after the order was placed. Use the order placement time.
Backfilled or late-arriving source data
Suppose a pipeline computes a feature at 15:00 but stamps it with event_timestamp of 10:00 because that is when the underlying event happened. A training row for 12:00 will see this value. At serving time at 12:00, the value did not exist yet. The training set is now optimistic compared with production. Stamp features with the time they became available, or accept and measure the difference.
Aggregates that look forward
A feature such as "trips in the 24-hour window centered on this hour" uses future data by construction. Check window definitions so they end at the feature timestamp.
Duplicate rows without a created timestamp
If two rows share a key and an event_timestamp and you did not configure created_timestamp_column, the winner is not defined by the rule above. Set the column so that corrections are chosen deterministically.
Label-derived features
A feature built from the label, such as "driver's acceptance rate including this trip", leaks even with a perfect join. The join cannot detect this. Review each feature for how it was computed.
Testing for leakage
A cheap regression test builds a small source table with a known future value, runs get_historical_features for an earlier timestamp, and asserts the earlier value comes back. Add a second case with an expired value and assert null. Run these tests whenever you change a source table, a timestamp column or a TTL.
You can also compare distributions. If a feature is much more predictive offline than the same feature measured on logged production requests, suspect leakage.
Exercise
A feature view has ttl=timedelta(hours=6). Source rows for driver 1001 have eventtimestamp 06:00 (value 0.4), 10:00 (value 0.5) and 15:00 (value 0.8). What does gethistorical_features return for driver 1001 at entity timestamps 09:00, 13:00 and 12:30 the next day? Then name one reason a team might still see leakage in this setup.
Show answer
At 09:00 the latest row at or before the timestamp is 06:00, which is 3 hours old and inside the TTL, so the value is 0.4. At 13:00 the 10:00 row is 3 hours old, so the value is 0.5. The 15:00 row is in the future and is ignored. At 12:30 the next day, the newest row at or before the timestamp is 15:00 the previous day, which is far older than 6 hours, so the result is null. Leakage can still occur if the 15:00 value was actually computed later and backdated, or if the entity timestamp is the label time rather than the prediction time.