Profile numeric columns by their mode to detect sentinel values in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `mode_report(df: pd.DataFrame) -> pd.DataFrame` that profiles each numeric column of `df` and reports its most frequent value or values. A large spike at one value often reveals a sentinel code such as `0`, `-1` or `999` standing in for missing or unknown data.
The output must follow these rules:
- One row per numeric column, sorted by `column` ascending, then `reset_index(drop=True)`.
- Columns in the order `['column', 'n_modes', 'min_mode', 'mode_freq', 'mode_pct', 'modality']`, with dtypes `object` for `column` and `modality`, `int64` for `n_modes` and `mode_freq`, and `float64` for `min_mode` and `mode_pct`.
- `n_modes` is `len(s.mode())` and `min_mode` is `s.mode().iloc[0]`, the smallest tied mode (`Series.mode()` returns all tied values sorted ascending).
- `mode_freq` is the number of rows holding `min_mode`. `mode_pct` is `mode_freq / s.count() * 100` rounded to 2 decimal places, so the denominator is the non-null count of the column and not `len(df)`.
- `modality` is `'Unimodal'` when `n_modes == 1`, else `'Tied modes'`. A column that is entirely NaN has no mode at all, and these rules leave its row unspecified (see the clarifying questions).
- The result is compared with `assert_frame_equal`, so column order and dtypes must match exactly.
For example:
```python
df = pd.DataFrame({
"balance": [0, 0, 0, 0, 120.5, 87.0, 310.0, 45.0, 99.0, 150.0],
"rating": [1, 1, 1, 2, 2, 2, 3, 4, 5, 5],
"age": [34, -1, 27, -1, np.nan, 41, -1, np.nan, 29, 52],
"city": ["Austin", "Boston", "Austin", "Denver", "Austin",
"Boston", "Denver", "Austin", "Boston", "Denver"],
})
```
returns:
```text
column n_modes min_mode mode_freq mode_pct modality
0 age 1 -1.0 3 37.5 Unimodal
1 balance 1 0.0 4 40.0 Unimodal
2 rating 2 1.0 3 30.0 Tied modes
```
`city` is not numeric, so it gets no row. `balance` is 40% zeros. `rating` has two modes, 1 and 2, with three rows each, and the smaller one is reported. `age` has 8 non-null values, so its three `-1` rows give 37.5 and not 30.0.
```hint Let pandas handle the ties
`Series.mode()` already drops NaN and returns every tied value in order. Build one record per column from it, then fix all the dtypes once at the end.
```
### Clarifying Questions to Ask
- What should the report show for a numeric column that is entirely NaN?
- Should boolean columns count as numeric?
### What a Strong Answer Covers
- Selects only the numeric columns and computes each mode with NaN ignored.
- Reports the smallest tied mode, the number of tied modes and the row count of that mode.
- Uses the non-null count as the denominator for `mode_pct` and rounds to 2 decimal places.
- Returns the exact column order, dtypes, sort order and index.
- Names the all-NaN column case and explains how the report helps spot sentinel values.
### Follow-up Questions
- How would you also flag a column whose `mode_pct` exceeds a threshold as a likely sentinel?
- What happens if a column is entirely NaN?
- A sentinel such as `999` can sit in 5% of rows without being the mode. How else would you find it?
- What does the report look like for a continuous column with no repeated values, and how should you read it?
Overview: Write a pandas function that reports each numeric column's most frequent value, how many modes tie and what share of non-null rows the mode holds, so spikes from sentinel codes such as 0 or -1 stand out. It tests Series.mode, NaN and tie handling, and exact output dtypes.