Build a daily metric time series per A/B test variant in pandas
Quick Overview
Turn event-level A/B test data into a long-format pandas DataFrame holding the daily mean of a metric for each variant. The task tests groupby aggregation, keeping group keys as columns, deterministic sorting, index resets and dtype preservation.
Build a daily metric time series per A/B test variant in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
You have event-level data from an A/B test and want a daily time series of a metric for each arm. Implement `daily_metric_by_variant(df)` in `solution.py`. `df` has one row per event with these columns:
- `variant` (object): `'control'` or `'treatment'`
- `date` (`datetime64[ns]`): the event date, normalized to midnight
- `value` (`float64`): the metric value for the event
Return the per-day mean of `value` for each variant as a long-format DataFrame.
Requirements:
- Group by `['variant', 'date']` and aggregate `value` with the mean.
- Return the columns `['variant', 'date', 'value']` in that order. Keep the long format and do not pivot to wide.
- Sort by `variant` ascending, then `date` ascending.
- Reset the index to `0..n-1`.
- Preserve dtypes: `variant` object, `date` `datetime64[ns]`, `value` `float64`.
Example:
```text
Input rows (variant, date, value)
control 2026-01-01 1.0
control 2026-01-01 3.0
treatment 2026-01-01 4.0
Output
variant date value
0 control 2026-01-01 2.0
1 treatment 2026-01-01 4.0
```
```hint Where the keys end up
By default a pandas groupby moves the grouping keys into the index. Decide how you get them back as ordinary columns, and check what the index looks like after you sort.
```
### What a Strong Answer Covers
- Grouping on both keys so each output row is exactly one variant-day, with the mean as the aggregate
- Keeping the keys as columns in the required order instead of a MultiIndex or a wide pivot
- A deterministic sort and a clean 0..n-1 index without the old index leaking in as a column
- Dtypes of the output and what happens with NaN values, missing keys or days with no events
- Time and space cost of the aggregation
### Follow-up Questions
- How would you fill in days where one variant logged no events, so both series cover the same dates?
- How would you add a per-day count and standard error so the plot can show confidence bands?
- If `date` held raw UTC timestamps but the readout should use the user's local day, what changes?
- Why can a daily mean over events mislead when the experiment randomizes users?
Overview: Turn event-level A/B test data into a long-format pandas DataFrame holding the daily mean of a metric for each variant. The task tests groupby aggregation, keeping group keys as columns, deterministic sorting, index resets and dtype preservation.
You have event-level data from an A/B test and want a daily time series of a metric for each arm. Implement daily_metric_by_variant(df) in solution.py. df has one row per event with these columns:
variant
(object):
'control'
or
'treatment'
date
(
datetime64[ns]
): the event date, normalized to midnight
value
(
float64
): the metric value for the event
Return the per-day mean of value for each variant as a long-format DataFrame.
Requirements:
Group by
['variant', 'date']
and aggregate
value
with the mean.
Return the columns
['variant', 'date', 'value']
in that order. Keep the long format and do not pivot to wide.