Bucketize NumPy values into half-open bins with np.searchsorted

Quick Overview

Map each value of a NumPy array to its bin index given strictly increasing edges, using one vectorized np.searchsorted call. It tests half-open bin boundaries, out-of-range values, the link to np.digitize, and the cost of binary search compared with a broadcast comparison.

Bucketize NumPy values into half-open bins with np.searchsorted

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a strictly increasing 1-D array `edges` and a 1-D array `values`, return an int array shaped like `values` with each value's bin index, using `np.searchsorted` (no Python loops). Requirements: - The result is `i` such that `edges[i-1] <= value < edges[i]` (use `side='right'`). - Values below `edges[0]` map to `0`, and values at or above `edges[-1]` map to `len(edges)`. - The result matches `np.digitize(values, edges, right=False)`. For example: ```text Input: edges = np.array([0, 10, 20, 30]) values = np.array([-5, 0, 5, 20, 35]) Output: [0, 1, 1, 3, 4] ``` -5 is below the first edge, so it maps to 0. 0 and 5 fall in `[0, 10)`, bin 1. 20 sits exactly on an edge and belongs to `[20, 30)`, bin 3. 35 is above the last edge, so it maps to `len(edges)`, which is 4. ```hint Ties at an edge Compare what `side='left'` and `side='right'` return for a value equal to an edge, such as 20 in the example, and check which one gives the required bin. ``` ### Constraints & Assumptions - `edges` is strictly increasing and has at least one element. - `values` may be empty (`len(values)` can be 0). - Values may lie outside the edge range. Input is finite (NaN is out of scope). ### What a Strong Answer Covers - One vectorized `np.searchsorted` call with the correct `side` argument. - How the returned index maps to the half-open bin convention, including values that sit exactly on an edge. - The two out-of-range cases, below the first edge and at or above the last. - Why the result matches `np.digitize`, and the cost compared with a broadcast comparison. ### Follow-up Questions - How would you change the code so bins are left-exclusive, right-inclusive? - How would you support a strictly decreasing `edges` array? - What is the time complexity, and why is `searchsorted` preferable to broadcasting a comparison matrix?

Overview: Map each value of a NumPy array to its bin index given strictly increasing edges, using one vectorized np.searchsorted call. It tests half-open bin boundaries, out-of-range values, the link to np.digitize, and the cost of binary search compared with a broadcast comparison.

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

Given a strictly increasing 1-D array edges and a 1-D array values, return an int array shaped like values with each value's bin index, using np.searchsorted (no Python loops).

Requirements:

  • The result is i such that edges[i-1] <= value < edges[i] (use side='right' ).
  • Values below edges[0] map to 0 , and values at or above edges[-1] map to len(edges) .
  • The result matches np.digitize(values, edges, right=False) .

For example:

Input:  edges  = np.array([0, 10, 20, 30])
        values = np.array([-5, 0, 5, 20, 35])
Output: [0, 1, 1, 3, 4]

-5 is below the first edge, so it maps to 0. 0 and 5 fall in [0, 10), bin 1. 20 sits exactly on an edge and belongs to [20, 30), bin 3. 35 is above the last edge, so it maps to len(edges), which is 4.

Constraints & Assumptions

  • edges is strictly increasing and has at least one element.
  • values may be empty ( len(values) can be 0).
  • Values may lie outside the edge range. Input is finite (NaN is out of scope).

What a Strong Answer Covers Guidance

  • One vectorized np.searchsorted call with the correct side argument.
  • How the returned index maps to the half-open bin convention, including values that sit exactly on an edge.
  • The two out-of-range cases, below the first edge and at or above the last.
  • Why the result matches np.digitize , and the cost compared with a broadcast comparison.

Follow-up Questions Guidance

  • How would you change the code so bins are left-exclusive, right-inclusive?
  • How would you support a strictly decreasing edges array?
  • What is the time complexity, and why is searchsorted preferable to broadcasting a comparison matrix?