scipp.rebin#

scipp.rebin(x: DataArray, arg_dict: Dict[str, int | Variable] = None, deprecated=None, /, **kwargs: int | Variable) DataArray#
scipp.rebin(x: Dataset, arg_dict: Dict[str, int | Variable] = None, deprecated=None, /, **kwargs: int | Variable) Dataset
scipp.rebin(x: DataGroup, arg_dict: Dict[str, int | Variable] = None, deprecated=None, /, **kwargs: int | Variable) DataGroup

Rebin a data array or dataset.

The coordinate of the input for the dimension to be rebinned must contain bin edges, i.e., the data must be histogrammed.

If the input has masks that contain the dimension being rebinned then those masks are applied to the data before rebinning. That is, masked values are treated as zero.

Parameters:
  • x – Data to rebin.

  • arg_dict – Dictionary mapping dimension labels to binning parameters.

  • **kwargs – Mapping of dimension label to corresponding binning parameters.

Returns:

Data rebinned according to the new bin edges.

See also

scipp.bin

For changing the binning of binned (as opposed to dense, histogrammed) data.

scipp.hist

For histogramming data.

Examples

Rebin a data array along one of its dimensions, specifying (1) number of bins, (2) bin width, or (3) actual binning:

>>> from numpy.random import default_rng
>>> rng = default_rng(seed=1234)
>>> x = sc.array(dims=['row'], unit='m', values=rng.random(100))
>>> y = sc.array(dims=['row'], unit='m', values=rng.random(100))
>>> data = sc.ones(dims=['row'], unit='K', shape=[100])
>>> table = sc.DataArray(data=data, coords={'x': x, 'y': y})
>>> da = table.hist(x=100, y=100)
>>> da.rebin(x=2).sizes
{'x': 2, 'y': 100}
>>> da.rebin(x=sc.scalar(0.2, unit='m')).sizes
{'x': 5, 'y': 100}
>>> da.rebin(x=sc.linspace('x', 0.2, 0.8, num=10, unit='m')).sizes
{'x': 9, 'y': 100}

Rebin a data array along two of its dimensions:

>>> da = table.hist(x=100, y=100)
>>> da.rebin(x=4, y=6).sizes
{'x': 4, 'y': 6}