scipp.bins_like#

scipp.bins_like(x, fill_value)#

Create a binned variable by “broadcasting” fill values to bins of given sizes.

The dimensions and shape of fill_value must be such that they can be broadcast to those of x. Each element of fill_value defines the values of all the bin elements of the corresponding bin. The output shares the bin indices of x.

Parameters:
  • x (VariableLike) – Binned variable or data array serving as prototype for bin sizes.

  • fill_value (Variable) – Fill values to use for the bins.

Returns:

Variable – Variable containing fill value in bins.

Examples

Create bins with constant fill values matching the structure of existing binned data:

>>> import scipp as sc
>>> binned = sc.data.binned_x(20, 3)
>>> binned.bins.size()
<scipp.DataArray>
Dimensions: Sizes[x:3, ]
Coordinates:
* x                         float64              [m]  (x [bin-edge])  [0.118091, 0.404294, 0.690497, 0.9767]
Data:
                              int64        <no unit>  (x)  [9, 4, 7]

Fill each bin with a different value:

>>> fill = sc.array(dims=['x'], values=[1.0, 2.0, 3.0], unit='K')
>>> sc.bins_like(binned.data, fill)
<scipp.Variable> (x: 3)  VariableView        <no unit>  binned data: dim='row', content=...

This is useful for initializing binned data structures with known values.