Forward fill each user's plan in time order without leaking across users
Why it matters Last observation carried forward is how slowly changing attributes are joined onto event logs for features and billing.
An events table only records a user's plan when it changes; other rows leave it blank. To label every event with the plan in effect, you carry the last known plan forward within that user only. Events before a user's first known plan get the default 'free'.
Write fill_plans(df) where df has columns user_id (int), ts (ISO timestamp string, string order is time order) and plan (str or None). Rows are in arbitrary order.
Return a list of str: the filled plan for each row, in the ORIGINAL row order. Fill forward in ts order within each user; use 'free' where no earlier plan exists for that user.
def fill_plans(df)Example
rows (1, '01', 'pro'), (1, '02', None), (2, '01', None), (2, '03', 'team') give ['pro', 'pro', 'free', 'team'].
Understand the idea
Forward fill (ffill) replaces a missing value with the last non-missing value above it. It assumes a value stays true until it changes, like a plan that stays 'pro' until the user switches.
Worked example: user 2 has events at 01 (None) and 03 ('team'). In time order the first row has nothing above it inside user 2, so it gets the default 'free'. If we filled the whole column, user 2's first row would wrongly inherit 'pro' from user 1.
Rule: sort by (key, time), then value_t = last known value at or before t within the same key, else default. In pandas: df.sort_values([key, time]).groupby(key)[col].ffill().fillna(default).
Feature stores use it for 'as of' attributes, and time series pipelines use it to fill sensor gaps per device.