Coding questions · Uber
Transform DataFrame and compute diff-in-diff
This question evaluates proficiency in data cleaning (type conversion), missing-data handling, group-period aggregation, and estimating treatment effects via difference-in-differences.
You are given a pandas DataFrame df with the following columns:
unit_id(string): entity identifier (e.g., user, city, driver)group(string): either'treatment'or'control'period(string): either'pre'or'post'y(string): outcome stored as a string (should be numeric), with exactly one missing value (NaN)
Tasks:
- Convert
yfrom string to integer (assume all non-missing values are valid integer strings, e.g.'12'). - Impute the missing value in
yusing the simple (unconditional) average of the non-missingyvalues. - After steps (1)–(2), compute the difference-in-differences (DiD) estimate of the treatment effect on
y:
Return the scalar DiD estimate (and optionally the intermediate group-period means used).
Transform DataFrame and compute diff-in-diff
Table df
| column | type | constraints |
|---|---|---|
| unit_id | VARCHAR | NOT NULL |
| group | VARCHAR | NOT NULL |
| period | VARCHAR | NOT NULL |
| y | VARCHAR |
Sample data:
| group | period | unit_id | y |
|---|---|---|---|
| treatment | pre | u1 | 10 |
| treatment | post | u1 | 15 |
| treatment | pre | u2 | 8 |
| treatment | post | u2 | 14 |
| control | pre | u3 | 9 |
| control | post | u3 | 11 |
| control | pre | u4 | NULL |
| control | post | u4 | 10 |
Expected output
After casting y to integer and imputing the single missing y with the overall mean of non-missing values, return the DiD estimate along with the group-period means used.
| ctrl_post_mean | ctrl_pre_mean | did_estimate | treat_post_mean | treat_pre_mean |
|---|---|---|---|---|
| 10.5 | 10 | 5 | 14.5 | 9 |
Hints
- CAST y from VARCHAR to INTEGER first; use AVG() which ignores NULLs to get the unconditional mean.
- Impute with COALESCE(cast(y_int as decimal), global_avg). Bring the global average to every row via CROSS JOIN.
- Quote the reserved column name "group" in SQL.
- Compute group-period means, then pivot with conditional aggregation to form the DiD components.