Detect Unused Categorical Levels in a pandas Series

Quick Overview

Write a pandas function that lists the declared levels of a categorical Series that never occur in the data, returned as a named int64 Series sorted by label. It tests how categorical dtypes report zero counts, label ordering, and matching exact names and dtypes.

Detect Unused Categorical Levels in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a `category` dtype Series, write `unused_categories_report(s: pd.Series) -> pd.Series` that reports the declared categories with zero observations. Requirements: - Index: the unused category labels as a plain `pd.Index` (without the categorical dtype), index name `category`. - Values: all int64 `0` (the observed count). Series name exactly `count`. - Sort by category label ascending. - If every declared category is observed, return an empty int64 Series with the same index name and Series name. ```python s = pd.Series(pd.Categorical(["a", "a", "c"], categories=["a", "b", "c", "d"])) unused_categories_report(s) # category # b 0 # d 0 # Name: count, dtype: int64 ``` The label order is the order of the label values, which can differ from the declared category order: ```python s = pd.Series(pd.Categorical(["low", "low"], categories=["low", "medium", "high"])) unused_categories_report(s) # category # high 0 # medium 0 # Name: count, dtype: int64 ``` ```hint Two places to look Check what `value_counts()` returns for a level that never occurs, and check which order `sort_index()` uses when the index is categorical. ``` ### Constraints & Assumptions - The input is always `category` dtype, built with `pd.Categorical(values, categories=[...])`. - The result is compared with `assert_series_equal`, so names, dtypes and the index type must match exactly. - All labels of one Series have the same type (all strings or all numbers), so they can be sorted. ### What a Strong Answer Covers - Where the zero-count levels come from: the dtype stores its categories separately from the data. - Sorting by label value, and why the declared category order can give a different result. - Producing the exact Series name, index name, value dtype and index type the comparison checks. - The empty case, including its dtype. ### Follow-up Questions - How would you also flag rare levels, observed but below a frequency threshold? - How would you drop dead levels so they stop polluting downstream stats? - Where else do unused categories show up as phantom rows, for example in `groupby`, and how do you control it?

Overview: Write a pandas function that lists the declared levels of a categorical Series that never occur in the data, returned as a named int64 Series sorted by label. It tests how categorical dtypes report zero counts, label ordering, and matching exact names and dtypes.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a category dtype Series, write unused_categories_report(s: pd.Series) -> pd.Series that reports the declared categories with zero observations.

Requirements:

  • Index: the unused category labels as a plain pd.Index (without the categorical dtype), index name category .
  • Values: all int64 0 (the observed count). Series name exactly count .
  • Sort by category label ascending.
  • If every declared category is observed, return an empty int64 Series with the same index name and Series name.
s = pd.Series(pd.Categorical(["a", "a", "c"], categories=["a", "b", "c", "d"]))
unused_categories_report(s)
# category
# b    0
# d    0
# Name: count, dtype: int64

The label order is the order of the label values, which can differ from the declared category order:

s = pd.Series(pd.Categorical(["low", "low"], categories=["low", "medium", "high"]))
unused_categories_report(s)
# category
# high      0
# medium    0
# Name: count, dtype: int64

Constraints & Assumptions

  • The input is always category dtype, built with pd.Categorical(values, categories=[...]) .
  • The result is compared with assert_series_equal , so names, dtypes and the index type must match exactly.
  • All labels of one Series have the same type (all strings or all numbers), so they can be sorted.

What a Strong Answer Covers Guidance

  • Where the zero-count levels come from: the dtype stores its categories separately from the data.
  • Sorting by label value, and why the declared category order can give a different result.
  • Producing the exact Series name, index name, value dtype and index type the comparison checks.
  • The empty case, including its dtype.

Follow-up Questions Guidance

  • How would you also flag rare levels, observed but below a frequency threshold?
  • How would you drop dead levels so they stop polluting downstream stats?
  • Where else do unused categories show up as phantom rows, for example in groupby , and how do you control it?