Schema Drift Diff Between Two DataFrame Extracts: Dtype Changes and Missing-Value Spikes

Quick Overview

Write a pandas function that compares two data extracts column by column and reports added and removed columns, dtype changes and spikes in missing values. It tests set logic over column names, per-frame null rates, rule precedence and exact control of output dtypes and ordering.

Schema Drift Diff Between Two DataFrame Extracts: Dtype Changes and Missing-Value Spikes

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `def schema_drift(df_old: pd.DataFrame, df_new: pd.DataFrame) -> pd.DataFrame` that compares two frames over the union of their column names. Return one row per column with exactly these columns, in order: `['column', 'dtype_old', 'dtype_new', 'missing_pct_old', 'missing_pct_new', 'status']`. The output columns are defined as follows: - `column` (object dtype) is the column name. - `dtype_old` and `dtype_new` hold the dtype string (for example `str(df[col].dtype)`), or `None` if the column is absent from that frame. Both are object columns. - `missing_pct_old` and `missing_pct_new` are `float64` values of `100 * nulls / len(frame)` rounded to 2 decimal places, or `NaN` if the column is absent from that frame. The two frames may differ in row count. - `status` (object) takes the first rule that matches: `added` (absent in old), `removed` (absent in new), `dtype_changed` (dtype strings differ), `missing_spike` (same dtype and `missing_pct_new - missing_pct_old > 10.0`), else `unchanged`. Sort by `status` ascending, then `column` ascending, then call `reset_index(drop=True)`. ```hint Order of the checks Some fields do not exist for a column that appears in only one frame. Decide what each rule needs before it can be evaluated, and let that guide the order you test them in. ``` ```hint Building the output Look at how pandas infers the dtype of a column that mixes strings with `None`, and make the object columns explicit instead of relying on inference. ``` ### Constraints & Assumptions - The two frames can have different row counts and different column sets. - Each missing percentage uses its own frame's row count as the denominator. - The percentages are rounded to 2 decimal places before the `missing_spike` comparison, so the rule fires on the same numbers the report shows. ### What a Strong Answer Covers - Building the union of column names and filling `None` and `NaN` for a column that exists in only one frame. - Missing percentages computed against each frame's own length and rounded before the threshold check. - The status rules applied in the stated precedence, with a reason the order matters. - The exact output column order and dtypes, including object columns that hold `None`. - The final sort by `status` then `column`, followed by an index reset. ### Follow-up Questions - How would you extend this to flag drift in category values or value distributions, in addition to dtypes and nulls? - Why round the percentages before the `missing_spike` comparison instead of after? - An integer column picks up a few nulls, pandas loads it as `float64`, and the report says `dtype_changed`. How would you make that case more useful to the person reading the report? - How would you produce the same report for two warehouse tables with hundreds of millions of rows each?

Overview: Write a pandas function that compares two data extracts column by column and reports added and removed columns, dtype changes and spikes in missing values. It tests set logic over column names, per-frame null rates, rule precedence and exact control of output dtypes and ordering.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write def schema_drift(df_old: pd.DataFrame, df_new: pd.DataFrame) -> pd.DataFrame that compares two frames over the union of their column names. Return one row per column with exactly these columns, in order: ['column', 'dtype_old', 'dtype_new', 'missing_pct_old', 'missing_pct_new', 'status'].

The output columns are defined as follows:

  • column (object dtype) is the column name.
  • dtype_old and dtype_new hold the dtype string (for example str(df[col].dtype) ), or None if the column is absent from that frame. Both are object columns.
  • missing_pct_old and missing_pct_new are float64 values of 100 * nulls / len(frame) rounded to 2 decimal places, or NaN if the column is absent from that frame. The two frames may differ in row count.
  • status (object) takes the first rule that matches: added (absent in old), removed (absent in new), dtype_changed (dtype strings differ), missing_spike (same dtype and missing_pct_new - missing_pct_old > 10.0 ), else unchanged .

Sort by status ascending, then column ascending, then call reset_index(drop=True).

Constraints & Assumptions

  • The two frames can have different row counts and different column sets.
  • Each missing percentage uses its own frame's row count as the denominator.
  • The percentages are rounded to 2 decimal places before the missing_spike comparison, so the rule fires on the same numbers the report shows.

What a Strong Answer Covers Guidance

  • Building the union of column names and filling None and NaN for a column that exists in only one frame.
  • Missing percentages computed against each frame's own length and rounded before the threshold check.
  • The status rules applied in the stated precedence, with a reason the order matters.
  • The exact output column order and dtypes, including object columns that hold None .
  • The final sort by status then column , followed by an index reset.

Follow-up Questions Guidance

  • How would you extend this to flag drift in category values or value distributions, in addition to dtypes and nulls?
  • Why round the percentages before the missing_spike comparison instead of after?
  • An integer column picks up a few nulls, pandas loads it as float64 , and the report says dtype_changed . How would you make that case more useful to the person reading the report?
  • How would you produce the same report for two warehouse tables with hundreds of millions of rows each?