digitize#

named_arrays.digitize(x, bins, axis=None, right=False)#

Thin wrapper around numpy.digitize().

Find the index of the bin along axis which each element of x falls into.

This function adds an axis argument, which lets bins carry axes besides the one it is monotonic along, where numpy.digitize() takes one-dimensional bins only. Those other axes are batch axes, and each is binned independently of the rest.

Parameters:
  • x (float | Quantity | AbstractArray) – The values to bin.

  • bins (AbstractArray) – The edges of the bins, monotonic along axis. They may increase or decrease, as numpy.digitize() allows.

  • axis (None | str) – The logical axis along which bins is monotonic. If None, bins must have exactly one axis.

  • right (bool) – Whether each bin includes its right edge instead of its left.

Returns:

  • The index of the bin each value falls into, carrying the axes of x

  • broadcast against the batch axes of bins.

  • An index of zero means the value falls below every bin, and an index

  • of the number of edges means it falls above every bin.

Return type:

AbstractArray

See also

numpy.digitize()

The function this one wraps.

searchsorted()

The insertion point of each value.

histogram()

The number of values in each bin.

Examples

Find which wavelength bin each of two spectral lines falls into.

import numpy as np
import astropy.units as u
import named_arrays as na

edges = na.linspace(100, 200, axis="wavelength", num=6) * u.AA

line = na.ScalarArray(
    ndarray=np.array([117, 185]) * u.AA,
    axes=("line",),
)

na.digitize(line, edges, axis="wavelength")
ScalarArray(
    ndarray=[1, 5],
    shape={'line': 2},
)