Category Share Shift Across Samples

Quick Overview

Write a pandas function that compares how a categorical column's level shares shift between a reference sample and a current sample, flagging new and dropped levels. It tests value counting with missing data, aligning on a union of levels, exact output dtypes and tie-aware sorting.

Category Share Shift Across Samples

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `category_share_shift(s_ref: pd.Series, s_cur: pd.Series) -> pd.DataFrame`, which compares the level shares of one categorical column between a reference sample and a current sample. The rows are the union of levels seen in either sample, with `NaN` values excluded from both samples. The result must meet these requirements: - Columns, in order: `level` (object), `share_ref`, `share_cur` and `abs_shift` (float64), `status` (object). - Shares are percentages (0 to 100) within each sample after dropping `NaN`. A level absent from a sample has share `0.0` there, never `NaN`. - `abs_shift` is `abs(share_cur - share_ref)`. - `status` is `'unseen_in_ref'` for a level that appears only in `s_cur`, `'dropped_in_cur'` for a level that appears only in `s_ref`, and `'shared'` otherwise. - Rows are sorted by `abs_shift` descending, then `level` ascending, with a fresh `RangeIndex`. The output is compared with `assert_frame_equal` using a float tolerance. ```python s_ref = pd.Series(['a', 'a', 'b', 'c']) # a: 50, b: 25, c: 25 s_cur = pd.Series(['a', 'b', 'b', 'd']) # a: 25, b: 50, d: 25 category_share_shift(s_ref, s_cur) # level share_ref share_cur abs_shift status # 0 a 50.0 25.0 25.0 shared # 1 b 25.0 50.0 25.0 shared # 2 c 25.0 0.0 25.0 dropped_in_cur # 3 d 0.0 25.0 25.0 unseen_in_ref ``` Every level moves by 25 points here, so the `level` tie-break decides the whole order. ```hint Per-sample shares pandas can give you each sample's shares directly. Once you have two share Series indexed by level, think about how to align them on the union of levels without leaving NaN behind. ``` ```hint Status before filling Record which sample each level came from before you replace missing shares with 0.0. Also check what a pandas categorical input does to the set of levels you count. ``` ### What a Strong Answer Covers - Computes each sample's shares as percentages of its own non-missing rows - Aligns both samples on the union of levels with 0.0 for absent levels, and assigns status from membership in each sample - Matches the column order, dtypes, the sort with its `level` tie-break, and a fresh `RangeIndex` - Handles edge cases such as an empty or all-NaN sample, categorical input with unused categories, and shifts that tie only up to floating-point error ### Follow-up Questions - How would you add a signed shift and a directional reading instead of only `abs_shift`? - How would you turn this report into a single drift score for the whole column? - With samples of very different sizes, how would you decide whether a shift is large enough to act on?

Overview: Write a pandas function that compares how a categorical column's level shares shift between a reference sample and a current sample, flagging new and dropped levels. It tests value counting with missing data, aligning on a union of levels, exact output dtypes and tie-aware sorting.

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

Implement category_share_shift(s_ref: pd.Series, s_cur: pd.Series) -> pd.DataFrame, which compares the level shares of one categorical column between a reference sample and a current sample. The rows are the union of levels seen in either sample, with NaN values excluded from both samples.

The result must meet these requirements:

  • Columns, in order: level (object), share_ref , share_cur and abs_shift (float64), status (object).
  • Shares are percentages (0 to 100) within each sample after dropping NaN . A level absent from a sample has share 0.0 there, never NaN .
  • abs_shift is abs(share_cur - share_ref) .
  • status is 'unseen_in_ref' for a level that appears only in s_cur , 'dropped_in_cur' for a level that appears only in s_ref , and 'shared' otherwise.
  • Rows are sorted by abs_shift descending, then level ascending, with a fresh RangeIndex .

The output is compared with assert_frame_equal using a float tolerance.

s_ref = pd.Series(['a', 'a', 'b', 'c'])   # a: 50, b: 25, c: 25
s_cur = pd.Series(['a', 'b', 'b', 'd'])   # a: 25, b: 50, d: 25

category_share_shift(s_ref, s_cur)
#   level  share_ref  share_cur  abs_shift          status
# 0     a       50.0       25.0       25.0          shared
# 1     b       25.0       50.0       25.0          shared
# 2     c       25.0        0.0       25.0  dropped_in_cur
# 3     d        0.0       25.0       25.0   unseen_in_ref

Every level moves by 25 points here, so the level tie-break decides the whole order.

What a Strong Answer Covers Guidance

  • Computes each sample's shares as percentages of its own non-missing rows
  • Aligns both samples on the union of levels with 0.0 for absent levels, and assigns status from membership in each sample
  • Matches the column order, dtypes, the sort with its level tie-break, and a fresh RangeIndex
  • Handles edge cases such as an empty or all-NaN sample, categorical input with unused categories, and shifts that tie only up to floating-point error

Follow-up Questions Guidance

  • How would you add a signed shift and a directional reading instead of only abs_shift ?
  • How would you turn this report into a single drift score for the whole column?
  • With samples of very different sizes, how would you decide whether a shift is large enough to act on?