Compute histogram bin counts in bin order with pandas pd.cut

Quick Overview

Write a pandas function that bins a numeric Series with fixed edges and returns the count per bin in bin order, the numbers behind a histogram. It tests pd.cut interval rules, how a categorical index sorts, handling of out-of-range values, and exact Series metadata.

Compute histogram bin counts in bin order with pandas pd.cut

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `histogram_counts(s: pd.Series, bin_edges: list[float]) -> pd.Series`. It bins a numeric Series and returns the count per bin in ascending bin order, which are the bar heights of a histogram from left to right. Return the Series only. Plotting with `matplotlib` is out of scope. Requirements: - Bin with `pd.cut(s, bins=bin_edges, right=True, include_lowest=True)`. `bin_edges` is sorted ascending. - Count with `.value_counts()`, then `.sort_index()`, so the result is in interval order and not in descending frequency. - Return an `int64` Series indexed by the ordered `CategoricalIndex` of interval bins. - Set the index name to `None` with `rename_axis(None)` and the Series `.name` to `'count'`. - Values outside `bin_edges` become `NaN` and are excluded from every bin. - The result is compared with `pd.testing.assert_series_equal(..., check_categorical=True)`, so the bin order and the ordered categorical dtype of the index must match. Example 1: ```text s = pd.Series([5, 12, 12, 25, 33, 0, 10, 15, 18, 20, 11, 21, 25, 27, 29, 30, 22, 35, 38, 40]) bin_edges = [0, 10, 20, 30, 40] histogram_counts(s, bin_edges) (-0.001, 10.0] 3 (10.0, 20.0] 6 (20.0, 30.0] 7 (30.0, 40.0] 4 Name: count, dtype: int64 ``` The first bin holds 0, 5 and 10: 10 falls in it because bins are closed on the right, and 0 falls in it because the lowest edge is included. The other bins hold 11, 12, 12, 15, 18, 20, then 21, 22, 25, 25, 27, 29, 30, then 33, 35, 38, 40. Example 2: ```text s = pd.Series([-5, 3, 8, 15, 45]) bin_edges = [0, 10, 20, 30, 40] histogram_counts(s, bin_edges) (-0.001, 10.0] 2 (10.0, 20.0] 1 (20.0, 30.0] 0 (30.0, 40.0] 0 Name: count, dtype: int64 ``` -5 and 45 are outside the edges and are not counted. The two bins with no values still appear, with a count of 0. ```hint Where the order comes from `value_counts()` sorts by frequency. Look at what kind of index its result has when the input comes from `pd.cut`, and how that index sorts. ``` ### What a Strong Answer Covers - Binning with the exact edge rules: intervals closed on the right, with the lowest edge included - Putting the counts in bin order, and explaining why the categorical index sorts by interval - Excluding out-of-range and missing values while keeping empty bins at zero - Matching the metadata: `int64` values, index name `None`, Series name `'count'` ### Follow-up Questions - Why does `sort_index()` put the bins in interval order instead of sorting the labels as text? - How would you count out-of-range values instead of dropping them? - How would you choose bin edges for a heavily skewed variable such as income?

Overview: Write a pandas function that bins a numeric Series with fixed edges and returns the count per bin in bin order, the numbers behind a histogram. It tests pd.cut interval rules, how a categorical index sorts, handling of out-of-range values, and exact Series metadata.

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

Implement histogram_counts(s: pd.Series, bin_edges: list[float]) -> pd.Series. It bins a numeric Series and returns the count per bin in ascending bin order, which are the bar heights of a histogram from left to right. Return the Series only. Plotting with matplotlib is out of scope.

Requirements:

  • Bin with pd.cut(s, bins=bin_edges, right=True, include_lowest=True) . bin_edges is sorted ascending.
  • Count with .value_counts() , then .sort_index() , so the result is in interval order and not in descending frequency.
  • Return an int64 Series indexed by the ordered CategoricalIndex of interval bins.
  • Set the index name to None with rename_axis(None) and the Series .name to 'count' .
  • Values outside bin_edges become NaN and are excluded from every bin.
  • The result is compared with pd.testing.assert_series_equal(..., check_categorical=True) , so the bin order and the ordered categorical dtype of the index must match.

Example 1:

s = pd.Series([5, 12, 12, 25, 33, 0, 10, 15, 18, 20,
               11, 21, 25, 27, 29, 30, 22, 35, 38, 40])
bin_edges = [0, 10, 20, 30, 40]

histogram_counts(s, bin_edges)

(-0.001, 10.0]    3
(10.0, 20.0]      6
(20.0, 30.0]      7
(30.0, 40.0]      4
Name: count, dtype: int64

The first bin holds 0, 5 and 10: 10 falls in it because bins are closed on the right, and 0 falls in it because the lowest edge is included. The other bins hold 11, 12, 12, 15, 18, 20, then 21, 22, 25, 25, 27, 29, 30, then 33, 35, 38, 40.

Example 2:

s = pd.Series([-5, 3, 8, 15, 45])
bin_edges = [0, 10, 20, 30, 40]

histogram_counts(s, bin_edges)

(-0.001, 10.0]    2
(10.0, 20.0]      1
(20.0, 30.0]      0
(30.0, 40.0]      0
Name: count, dtype: int64

-5 and 45 are outside the edges and are not counted. The two bins with no values still appear, with a count of 0.

What a Strong Answer Covers Guidance

  • Binning with the exact edge rules: intervals closed on the right, with the lowest edge included
  • Putting the counts in bin order, and explaining why the categorical index sorts by interval
  • Excluding out-of-range and missing values while keeping empty bins at zero
  • Matching the metadata: int64 values, index name None , Series name 'count'

Follow-up Questions Guidance

  • Why does sort_index() put the bins in interval order instead of sorting the labels as text?
  • How would you count out-of-range values instead of dropping them?
  • How would you choose bin edges for a heavily skewed variable such as income?