Fill missing latencies with each model's median and a global fallback
Why it matters Group-aware imputation is a standard feature engineering step and avoids mixing fast and slow segments.
Some log rows lose their latency value. Filling with one global number blurs fast and slow models together; filling with each model's median keeps them apart. A model with no known values at all still needs a fallback.
Write fill_latency(df) where df has columns model (str) and latency_ms (float, may be NaN).
Return a list of floats, the latency_ms column in the original row order, where each NaN is replaced by the median of the known latencies of the same model, or by the median of ALL known latencies if that model has none.
def fill_latency(df)Example
model ['a', 'a', 'a', 'b', 'c'], latency [10, 30, NaN, 100, NaN] gives [10.0, 30.0, 20.0, 100.0, 30.0].
Understand the idea
fillna replaces missing values. The fill can be a constant, a per-row Series, or a forward fill. A good default for skewed numbers like latency is the median of a similar group, because one huge value does not move the median.
Worked example: model a has 10 and 30 known, so its median is 20 and its NaN becomes 20. Model c has no known value, so it falls back to the median of all known values: 10, 30, 100, whose median is 30.
Rule: x_filled = x, else median(x in same group), else median(all known x). In pandas: s.fillna(df.groupby(g)[col].transform('median')).fillna(s.median()).
Feature pipelines for tabular models and latency dashboards use group-wise median imputation with a global fallback.