Build a leak-free date feature transformer with fit-time median imputation

Quick Overview

Build a scikit-learn style transformer that turns signup, event and purchase timestamps into day-count and calendar features without leaking future or test-set information. It tests datetime parsing, handling of impossible values, fit-time median imputation, missing flags and exact output dtypes.

Build a leak-free date feature transformer with fit-time median imputation

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit`, `transform` and `fit_transform` on `DateFeatureTransformer`. Inputs are a DataFrame with signup, event and last-purchase timestamp columns (datetimes or strings) and a fixed `reference_date`. `transform` returns a new DataFrame with exactly `OUTPUT_COLUMNS`, in that order, keeping the input index. ```python class DateFeatureTransformer: OUTPUT_COLUMNS = [ "account_age_days", "days_since_last_purchase", "last_purchase_missing", "is_weekend", "is_month_end", ] def __init__(self, reference_date, signup_col="signup_ts", event_col="event_ts", purchase_col="last_purchase_ts"): ... ``` The features are defined as follows. - `account_age_days` is (event minus signup) in days. `days_since_last_purchase` is (`reference_date` minus purchase) in days. Negative or unparseable values count as missing. - `fit` stores the medians of the valid values in `account_age_median_` and `days_since_purchase_median_` (`0.0` if there are none) and returns `self`. `transform` fills missing values with these medians. - `last_purchase_missing` is 1 when the days-since value is missing, including purchases after `reference_date`. - `is_weekend` (Saturday or Sunday) and `is_month_end` are computed from the event date. A missing event gives 0. - The two day columns are `float64` and the three flags are `int64`. Never mutate the input. Example: ```text reference_date = "2024-01-31" signup_ts = ["2024-01-01", "2024-01-10", None] event_ts = ["2024-01-11", "2024-01-13", "2024-01-31"] last_purchase_ts = ["2024-01-05", None, "2024-02-10"] fit_transform(X) returns account_age_days [10.0, 3.0, 6.5] days_since_last_purchase [26.0, 26.0, 26.0] last_purchase_missing [0, 1, 1] is_weekend [0, 1, 0] is_month_end [0, 0, 1] ``` ```hint Order of operations Filling a gap erases the evidence that it was a gap. Decide when each output column has to be computed. ``` ### What a Strong Answer Covers - Parsing strings and datetimes into timestamps, with unparseable values becoming missing - Treating negative ages and purchases after the reference date as missing, and setting the missing flag before imputation - Learning both medians in `fit` only, with the `0.0` fallback, and reusing them in `transform` - Weekend and month-end flags from the event date, with 0 for a missing event - The exact output columns, order, dtypes and index, with the input left unchanged ### Follow-up Questions - How would you change the transformer if each row had its own prediction time instead of one fixed `reference_date`? - The timestamps come from users in several time zones. How does that affect the day differences and the weekend flag? - Why keep a `last_purchase_missing` flag when the gap is already filled with the median?

Overview: Build a scikit-learn style transformer that turns signup, event and purchase timestamps into day-count and calendar features without leaking future or test-set information. It tests datetime parsing, handling of impossible values, fit-time median imputation, missing flags and exact output dtypes.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement fit, transform and fit_transform on DateFeatureTransformer. Inputs are a DataFrame with signup, event and last-purchase timestamp columns (datetimes or strings) and a fixed reference_date. transform returns a new DataFrame with exactly OUTPUT_COLUMNS, in that order, keeping the input index.

class DateFeatureTransformer:
    OUTPUT_COLUMNS = [
        "account_age_days",
        "days_since_last_purchase",
        "last_purchase_missing",
        "is_weekend",
        "is_month_end",
    ]

    def __init__(self, reference_date, signup_col="signup_ts",
                 event_col="event_ts", purchase_col="last_purchase_ts"):
        ...

The features are defined as follows.

  • account_age_days is (event minus signup) in days. days_since_last_purchase is ( reference_date minus purchase) in days. Negative or unparseable values count as missing.
  • fit stores the medians of the valid values in account_age_median_ and days_since_purchase_median_ ( 0.0 if there are none) and returns self . transform fills missing values with these medians.
  • last_purchase_missing is 1 when the days-since value is missing, including purchases after reference_date .
  • is_weekend (Saturday or Sunday) and is_month_end are computed from the event date. A missing event gives 0.
  • The two day columns are float64 and the three flags are int64 . Never mutate the input.

Example:

reference_date   = "2024-01-31"
signup_ts        = ["2024-01-01", "2024-01-10", None]
event_ts         = ["2024-01-11", "2024-01-13", "2024-01-31"]
last_purchase_ts = ["2024-01-05", None,         "2024-02-10"]

fit_transform(X) returns
account_age_days         [10.0, 3.0, 6.5]
days_since_last_purchase [26.0, 26.0, 26.0]
last_purchase_missing    [0, 1, 1]
is_weekend               [0, 1, 0]
is_month_end             [0, 0, 1]

What a Strong Answer Covers Guidance

  • Parsing strings and datetimes into timestamps, with unparseable values becoming missing
  • Treating negative ages and purchases after the reference date as missing, and setting the missing flag before imputation
  • Learning both medians in fit only, with the 0.0 fallback, and reusing them in transform
  • Weekend and month-end flags from the event date, with 0 for a missing event
  • The exact output columns, order, dtypes and index, with the input left unchanged

Follow-up Questions Guidance

  • How would you change the transformer if each row had its own prediction time instead of one fixed reference_date ?
  • The timestamps come from users in several time zones. How does that affect the day differences and the weekend flag?
  • Why keep a last_purchase_missing flag when the gap is already filled with the median?