Report hidden missing values (NaN plus text sentinels) in pandas object columns

Quick Overview

Write a pandas function that reports, for every text column, how many cells are truly missing and how many hold placeholder strings such as empty text or NA. It tests exact matching against a sentinel list, avoiding double counts, and building a sorted report with fixed dtypes.

Report hidden missing values (NaN plus text sentinels) in pandas object columns

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `hidden_missing_report(df, sentinels)`, which returns a pandas DataFrame counting true missing values plus text sentinels (placeholder strings such as `''` or `'NA'`) for every object-dtype column of `df`. The report must follow these rules: - One row per object-dtype column (`df.select_dtypes(include='object')`). Numeric and datetime columns are ignored. - Columns are exactly `['column', 'n_isna', 'n_sentinel', 'n_hidden_total', 'pct_hidden_total']`: - `n_isna` (`int64`) counts cells caught by `isna()`. - `n_sentinel` (`int64`) counts cells whose raw value exactly equals a string in `sentinels` (case-sensitive, no stripping). - `n_hidden_total` (`int64`) is `n_isna + n_sentinel`. - `pct_hidden_total` (`float64`) is `n_hidden_total / len(df) * 100`. - Rows are sorted by `n_hidden_total` descending, then `column` ascending, and the index is reset with `reset_index(drop=True)`. - `df` is not modified, and sentinels are not replaced with `NaN`. The function returns only the report. - The result is compared with `assert_frame_equal`, so dtypes and column order must match. Example with `sentinels=['', 'NA', 'N/A', '?', 'unknown']`: ```python import numpy as np import pandas as pd df = pd.DataFrame({ "city": pd.Series(["Austin", "", None, "NA"], dtype=object), "plan": pd.Series(["pro", "basic", "n/a", "unknown"], dtype=object), "note": pd.Series([" NA", "ok", np.nan, "?"], dtype=object), "age": [31, -999, 40, 22], }) ``` ```text column n_isna n_sentinel n_hidden_total pct_hidden_total 0 city 1 2 3 75.0 1 note 1 1 2 50.0 2 plan 0 1 1 25.0 ``` `age` is numeric (`int64`), so it is not reported even though `-999` looks like a placeholder. `'n/a'` and `' NA'` do not match any sentinel exactly, so they are not counted. ```hint Two separate counts `isna()` sees only real missing values, so the text sentinels need their own exact-match count. Before adding the two counts, check whether any cell could land in both. ``` ### Constraints & Assumptions - Text columns in `df` are object dtype. On pandas 3.0 or later, string inference gives plain string lists the `str` dtype instead, so the example builds its text columns with `dtype=object`. - `sentinels` is a list of strings. The grader passes `['', 'NA', 'N/A', '?', 'unknown']`. - Sentinels and `NaN` are mutually exclusive, so there is no double counting. - Only object-dtype columns are reported, so bool and category columns are skipped as well. - Column names are strings. - When `df` has zero rows, `pct_hidden_total` is 0.0. ### What a Strong Answer Covers - Why `isna()` misses disguised missing values, and how the sentinel count is kept separate from it. - Exact, case-sensitive matching with no stripping, and why no cell is counted twice. - The five columns in the required order and dtypes, including when there are no object columns or no rows. - The two-key sort and reset index the grader compares. - Leaving `df` unchanged. ### Follow-up Questions - How would you handle sentinels that differ only by surrounding whitespace or case? - How would you extend this to flag disguised missing values in numeric columns, such as `-999` or `0`? - How would you find sentinel values in a new dataset when nobody has given you the list?

Overview: Write a pandas function that reports, for every text column, how many cells are truly missing and how many hold placeholder strings such as empty text or NA. It tests exact matching against a sentinel list, avoiding double counts, and building a sorted report with fixed dtypes.

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

Write hidden_missing_report(df, sentinels), which returns a pandas DataFrame counting true missing values plus text sentinels (placeholder strings such as '' or 'NA') for every object-dtype column of df.

The report must follow these rules:

  • One row per object-dtype column ( df.select_dtypes(include='object') ). Numeric and datetime columns are ignored.
  • Columns are exactly ['column', 'n_isna', 'n_sentinel', 'n_hidden_total', 'pct_hidden_total'] :
    • n_isna ( int64 ) counts cells caught by isna() .
    • n_sentinel ( int64 ) counts cells whose raw value exactly equals a string in sentinels (case-sensitive, no stripping).
    • n_hidden_total ( int64 ) is n_isna + n_sentinel .
    • pct_hidden_total ( float64 ) is n_hidden_total / len(df) * 100 .
  • Rows are sorted by n_hidden_total descending, then column ascending, and the index is reset with reset_index(drop=True) .
  • df is not modified, and sentinels are not replaced with NaN . The function returns only the report.
  • The result is compared with assert_frame_equal , so dtypes and column order must match.

Example with sentinels=['', 'NA', 'N/A', '?', 'unknown']:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    "city": pd.Series(["Austin", "", None, "NA"], dtype=object),
    "plan": pd.Series(["pro", "basic", "n/a", "unknown"], dtype=object),
    "note": pd.Series([" NA", "ok", np.nan, "?"], dtype=object),
    "age":  [31, -999, 40, 22],
})
  column  n_isna  n_sentinel  n_hidden_total  pct_hidden_total
0   city       1           2               3              75.0
1   note       1           1               2              50.0
2   plan       0           1               1              25.0

age is numeric (int64), so it is not reported even though -999 looks like a placeholder. 'n/a' and ' NA' do not match any sentinel exactly, so they are not counted.

Constraints & Assumptions

  • Text columns in df are object dtype. On pandas 3.0 or later, string inference gives plain string lists the str dtype instead, so the example builds its text columns with dtype=object .
  • sentinels is a list of strings. The grader passes ['', 'NA', 'N/A', '?', 'unknown'] .
  • Sentinels and NaN are mutually exclusive, so there is no double counting.
  • Only object-dtype columns are reported, so bool and category columns are skipped as well.
  • Column names are strings.
  • When df has zero rows, pct_hidden_total is 0.0.

What a Strong Answer Covers Guidance

  • Why isna() misses disguised missing values, and how the sentinel count is kept separate from it.
  • Exact, case-sensitive matching with no stripping, and why no cell is counted twice.
  • The five columns in the required order and dtypes, including when there are no object columns or no rows.
  • The two-key sort and reset index the grader compares.
  • Leaving df unchanged.

Follow-up Questions Guidance

  • How would you handle sentinels that differ only by surrounding whitespace or case?
  • How would you extend this to flag disguised missing values in numeric columns, such as -999 or 0 ?
  • How would you find sentinel values in a new dataset when nobody has given you the list?