Point-in-Time Correct As-Of Join
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `point_in_time_join(events, features, entity_col, event_time_col, feature_time_col, feature_cols=None, tolerance=None)`. For each event, attach the feature values from the latest `features` row of the same entity with `feature_time <= event_time`. Return a copy of `events` (same row order and index) with one `float64` column per feature appended. Do not loop row by row.
Rules:
- `feature_cols=None` means every column of `features` except the entity and time columns. Otherwise append the columns in the given order.
- Ties on `feature_time`: the row that appears last in `features` wins. Inputs may be unsorted.
- `tolerance` (a Timedelta or a string such as `"2D"`): a match is kept only if `event_time - feature_time <= tolerance` (inclusive). Otherwise the event gets NaN.
- Feature rows with a null time or entity are ignored. Events with a null time or entity, or with no match, get NaN.
- A feature column that already exists in `events` raises `ValueError`. The inputs must not be mutated.
Example:
```python
features = pd.DataFrame({
"entity_id": ["a", "a", "b", "a"],
"feature_time": pd.to_datetime(["2024-01-01", "2024-01-03", "2024-01-02", "2024-01-05"]),
"f1": [1.0, 2.0, 10.0, 3.0],
"f2": [5, 6, 7, 8],
})
events = pd.DataFrame({
"entity_id": ["a", "a", "b", "c", "a"],
"event_time": pd.to_datetime(["2024-01-02", "2024-01-03", "2024-01-01", "2024-01-04", "2024-01-10"]),
})
out = point_in_time_join(events, features, "entity_id", "event_time", "feature_time")
# out["f1"]: [1.0, 2.0, NaN, NaN, 3.0]
# out["f2"]: [5.0, 6.0, NaN, NaN, 8.0]
```
The first `a` event sees only the 2024-01-01 row. The second sees the 2024-01-03 row because the match is inclusive. The `b` event happens before `b`'s only feature row, and `c` has no features at all, so both get NaN. The last `a` event picks up the 2024-01-05 row.
With `tolerance="2D"` and the same inputs, the last event's match is 5 days old and is dropped, so `f1` becomes `[1.0, 2.0, NaN, NaN, NaN]`.
```hint One sort for both tables
Put the feature rows and the event rows into one set of keys and sort it once. Choose the sort keys so that the rows an event may use end up just before it, and think about the order at equal timestamps.
```
```hint Finding the last feature row without a loop
After the sort, each event needs the position of the most recent feature row before it. A cumulative operation over positions can give you that, but watch where it points when an entity has no earlier feature row.
```
### What a Strong Answer Covers
- A vectorized method (a combined sort with a running maximum, or a carefully prepared `merge_asof`) with no per-row Python loop
- Inclusive matching at equal timestamps and the last-row-wins rule for duplicate feature times, both enforced by the sort order
- Entity boundaries, null keys, unmatched events and the inclusive `tolerance` check, each producing NaN where the rules say so
- The output contract: a copy of `events` in its original order and index, `float64` columns in `feature_cols` order, the collision check, and untouched inputs
- Why the result cannot leak future information, and the time and space cost
### Follow-up Questions
- How would this scale to billions of rows in a feature store?
- What if features have both an event timestamp and an ingestion (created) timestamp?
- How would you change the join so that each feature column takes its own latest non-null value?
Overview: Implement a point-in-time correct as-of join that attaches, for each event, the latest feature values its entity had at that moment, without a row-by-row loop. It tests leakage-free training data construction, tie and tolerance rules, null handling and vectorized sorting with NumPy and pandas.