cswork / libraryOpen practice workspace

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:

  1. Convert y from string to integer (assume all non-missing values are valid integer strings, e.g. '12').
  2. Impute the missing value in y using the simple (unconditional) average of the non-missing y values.
  3. After steps (1)–(2), compute the difference-in-differences (DiD) estimate of the treatment effect on y:

DiD=(y‾treat, post−y‾treat, pre)−(y‾ctrl, post−y‾ctrl, pre)\text{DiD} = (\overline{y}_{\text{treat, post}} - \overline{y}_{\text{treat, pre}}) - (\overline{y}_{\text{ctrl, post}} - \overline{y}_{\text{ctrl, pre}})

Return the scalar DiD estimate (and optionally the intermediate group-period means used).

Transform DataFrame and compute diff-in-diff

Table df

columntypeconstraints
unit_idVARCHARNOT NULL
groupVARCHARNOT NULL
periodVARCHARNOT NULL
yVARCHAR

Sample data:

groupperiodunit_idy
treatmentpreu110
treatmentpostu115
treatmentpreu28
treatmentpostu214
controlpreu39
controlpostu311
controlpreu4NULL
controlpostu410

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_meanctrl_pre_meandid_estimatetreat_post_meantreat_pre_mean
10.510514.59

Hints

  1. CAST y from VARCHAR to INTEGER first; use AVG() which ignores NULLs to get the unconditional mean.
  2. Impute with COALESCE(cast(y_int as decimal), global_avg). Bring the global average to every row via CROSS JOIN.
  3. Quote the reserved column name "group" in SQL.
  4. Compute group-period means, then pivot with conditional aggregation to form the DiD components.