Screen categorical features for perfectly separating levels (target leakage) in pandas

Quick Overview

Write a pandas function that scans categorical columns for levels whose binary target rate is exactly 0 or 1 and reports how many rows those levels cover. It tests leakage screening, exact comparisons on proportions, handling of missing values and unused categories, and returning a stable empty schema.

Screen categorical features for perfectly separating levels (target leakage) in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `perfect_separation_categories(df, target)` to flag the object and category columns (excluding `target`) that contain at least one pure level: a level whose target rate is exactly `0.0` or exactly `1.0`. `target` is binary and present in `df`. A perfectly separating level is a common sign of leakage, for example an ID column or a relabeled copy of the outcome. Requirements: - The columns are exactly `['feature', 'n_levels', 'n_pure_levels', 'rows_covered', 'pct_rows_pure']`. - `n_levels` (distinct levels), `n_pure_levels` and `rows_covered` (rows in pure levels) are `int64`. `pct_rows_pure` is `100 * rows_covered / len(df)` as `float64`. - Include only features with at least one pure level. A lopsided rate such as 0.98 is not pure. - Sort by `pct_rows_pure` descending, then `feature` ascending, then `reset_index(drop=True)`. - If nothing is flagged, return an empty frame with the same schema and dtypes. ```hint Exact means exact Think about which values the mean of 0/1 integers can take at the boundaries, and whether a tolerance-based comparison could let a lopsided level through. ``` ```hint The empty case The schema has to hold even when no feature is flagged. Check which dtypes an empty DataFrame gets when you do not specify them. ``` ### Constraints & Assumptions - `target` is a 0/1 column with no missing values. - Candidate features are the columns with `object` or `category` dtype. Numeric columns are out of scope. - A missing value in a feature is not a level. Those rows count toward `len(df)` but not toward `n_levels` or `rows_covered`. - For a `category` column, only levels that occur in the data count toward `n_levels`. ### What a Strong Answer Covers - Selecting the object and category columns and skipping the target - Testing purity exactly, so a lopsided rate such as 0.98 or 0.99999 is never flagged - Computing coverage over `len(df)` and treating missing feature values and unused categories deliberately - Sorting with the tie-break and returning the same schema and dtypes when nothing is flagged - Explaining what a flagged feature means for leakage and when the screen raises false alarms ### Follow-up Questions - How would tiny or rare levels distort this screen, and how would you guard against it? - Why screen categoricals separately from a numeric AUC or correlation check? - How would you extend the screen to numeric columns or to pairs of categorical columns?

Overview: Write a pandas function that scans categorical columns for levels whose binary target rate is exactly 0 or 1 and reports how many rows those levels cover. It tests leakage screening, exact comparisons on proportions, handling of missing values and unused categories, and returning a stable empty schema.

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

Implement perfect_separation_categories(df, target) to flag the object and category columns (excluding target) that contain at least one pure level: a level whose target rate is exactly 0.0 or exactly 1.0. target is binary and present in df. A perfectly separating level is a common sign of leakage, for example an ID column or a relabeled copy of the outcome.

Requirements:

  • The columns are exactly ['feature', 'n_levels', 'n_pure_levels', 'rows_covered', 'pct_rows_pure'] .
  • n_levels (distinct levels), n_pure_levels and rows_covered (rows in pure levels) are int64 . pct_rows_pure is 100 * rows_covered / len(df) as float64 .
  • Include only features with at least one pure level. A lopsided rate such as 0.98 is not pure.
  • Sort by pct_rows_pure descending, then feature ascending, then reset_index(drop=True) .
  • If nothing is flagged, return an empty frame with the same schema and dtypes.

Constraints & Assumptions

  • target is a 0/1 column with no missing values.
  • Candidate features are the columns with object or category dtype. Numeric columns are out of scope.
  • A missing value in a feature is not a level. Those rows count toward len(df) but not toward n_levels or rows_covered .
  • For a category column, only levels that occur in the data count toward n_levels .

What a Strong Answer Covers Guidance

  • Selecting the object and category columns and skipping the target
  • Testing purity exactly, so a lopsided rate such as 0.98 or 0.99999 is never flagged
  • Computing coverage over len(df) and treating missing feature values and unused categories deliberately
  • Sorting with the tie-break and returning the same schema and dtypes when nothing is flagged
  • Explaining what a flagged feature means for leakage and when the screen raises false alarms

Follow-up Questions Guidance

  • How would tiny or rare levels distort this screen, and how would you guard against it?
  • Why screen categoricals separately from a numeric AUC or correlation check?
  • How would you extend the screen to numeric columns or to pairs of categorical columns?