Detect Modality From Histogram Bin Counts in pandas

Quick Overview

Implement a pandas function that bins a numeric Series, finds the peak bins in the counts and labels the distribution as unimodal, bimodal or multimodal. It tests binning with pd.cut, keeping counts in bin order, local-maximum logic and exact output dtypes.

Detect Modality From Histogram Bin Counts in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `modality_report(s: pd.Series, bin_edges: list[float], labels: list[str]) -> pd.DataFrame` that bins the series, finds peak bins from the counts, and reports the modality verdict. The function must follow these rules: - Bin with `pd.cut(s, bins=bin_edges, labels=labels, right=True, include_lowest=True)` and count in ascending bin order via `.value_counts().sort_index()`, not in frequency order. - A peak bin has a count strictly greater than both immediate neighbours. The first and last bins compare against their single neighbour. - One peak gives `'Unimodal'`, two give `'Bimodal'`, and 3 or more give `'Multimodal'`. The verdict is repeated on every row. - Return one row per peak bin with columns exactly `['bin_label', 'count', 'modality']`, sorted by `count` descending, then `bin_label` ascending, then `reset_index(drop=True)`. - `bin_label` and `modality` have dtype `object` (plain `str`, not Categorical) and `count` has dtype `int64`. For example: ```python s = pd.Series([0, 2, 5, 8, 10, 15, 22, 30, 31, 33, 35, 38, 40]) bin_edges = [0, 10, 20, 30, 40] labels = ["0-10", "10-20", "20-30", "30-40"] ``` ```text bin_label count modality 0 0-10 5 Bimodal 1 30-40 5 Bimodal ``` In bin order the counts are 5, 1, 2, 5 (the value 0 lands in the first bin because of `include_lowest=True`, and 10, 30 and 40 land in the bin they close). The first and last bins beat their single neighbour, while `20-30` beats `10-20` but not `30-40`, so there are two peaks. They tie on count, and `'0-10'` sorts before `'30-40'`. ```hint Keep the bins in place Check which order `value_counts()` returns by default, and whether bins that received no values still appear in the result. ``` ```hint Output dtypes `pd.cut` with labels returns a Categorical. Think about what that does to the dtype of `bin_label` when you build the report. ``` ### Constraints & Assumptions - `bin_edges` is strictly increasing and `labels` has exactly `len(bin_edges) - 1` unique entries. - The input always produces at least one peak bin. ### Clarifying Questions to Ask - Do bins with a count of zero take part in the neighbour comparison? - How should a flat-topped hump be treated, where two adjacent bins tie for the highest count? ### What a Strong Answer Covers - Counting in bin order with every bin present, including empty ones, so neighbours are physically adjacent. - Applying the strict local-maximum rule, with the first and last bins compared against one neighbour. - Mapping the number of peaks to the verdict and repeating it on every row. - Producing the exact columns, sort order and plain `object` and `int64` dtypes. - Explaining what the strict rule does with plateaus and why the bin edges affect the verdict. ### Follow-up Questions - How would you keep peak detection from reporting sampling noise in jagged counts as extra modes? - Why can the choice of `bin_edges` change the modality verdict? - How would you change the rule so that a flat-topped hump counts as one peak?

Overview: Implement a pandas function that bins a numeric Series, finds the peak bins in the counts and labels the distribution as unimodal, bimodal or multimodal. It tests binning with pd.cut, keeping counts in bin order, local-maximum logic and exact output dtypes.

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

Implement modality_report(s: pd.Series, bin_edges: list[float], labels: list[str]) -> pd.DataFrame that bins the series, finds peak bins from the counts, and reports the modality verdict.

The function must follow these rules:

  • Bin with pd.cut(s, bins=bin_edges, labels=labels, right=True, include_lowest=True) and count in ascending bin order via .value_counts().sort_index() , not in frequency order.
  • A peak bin has a count strictly greater than both immediate neighbours. The first and last bins compare against their single neighbour.
  • One peak gives 'Unimodal' , two give 'Bimodal' , and 3 or more give 'Multimodal' . The verdict is repeated on every row.
  • Return one row per peak bin with columns exactly ['bin_label', 'count', 'modality'] , sorted by count descending, then bin_label ascending, then reset_index(drop=True) .
  • bin_label and modality have dtype object (plain str , not Categorical) and count has dtype int64 .

For example:

s = pd.Series([0, 2, 5, 8, 10, 15, 22, 30, 31, 33, 35, 38, 40])
bin_edges = [0, 10, 20, 30, 40]
labels = ["0-10", "10-20", "20-30", "30-40"]
  bin_label  count modality
0      0-10      5  Bimodal
1     30-40      5  Bimodal

In bin order the counts are 5, 1, 2, 5 (the value 0 lands in the first bin because of include_lowest=True, and 10, 30 and 40 land in the bin they close). The first and last bins beat their single neighbour, while 20-30 beats 10-20 but not 30-40, so there are two peaks. They tie on count, and '0-10' sorts before '30-40'.

Constraints & Assumptions

  • bin_edges is strictly increasing and labels has exactly len(bin_edges) - 1 unique entries.
  • The input always produces at least one peak bin.

Clarifying Questions to Ask Guidance

  • Do bins with a count of zero take part in the neighbour comparison?
  • How should a flat-topped hump be treated, where two adjacent bins tie for the highest count?

What a Strong Answer Covers Guidance

  • Counting in bin order with every bin present, including empty ones, so neighbours are physically adjacent.
  • Applying the strict local-maximum rule, with the first and last bins compared against one neighbour.
  • Mapping the number of peaks to the verdict and repeating it on every row.
  • Producing the exact columns, sort order and plain object and int64 dtypes.
  • Explaining what the strict rule does with plateaus and why the bin edges affect the verdict.

Follow-up Questions Guidance

  • How would you keep peak detection from reporting sampling noise in jagged counts as extra modes?
  • Why can the choice of bin_edges change the modality verdict?
  • How would you change the rule so that a flat-topped hump counts as one peak?