I think you are reinventing `np.digitize`, which is [implemented in C](https://github.com/numpy/numpy/blob/3abd8699dc3c71e389356ca6d80a2cb9efa16151/numpy/lib/src/_compiled_base.c#L261) and so may be faster.