scipp.GroupByDataArray#
- class scipp.GroupByDataArray#
GroupBy object implementing split-apply-combine mechanism.
- __init__(*args, **kwargs)#
Methods
__init__(*args, **kwargs)all(self, dim)Element-wise all over the specified dimension within a group.
any(self, dim)Element-wise any over the specified dimension within a group.
concat(self, dim)Concatenate bins within each group.
max(self, dim)Element-wise max over the specified dimension within a group.
mean(self, dim)Element-wise mean over the specified dimension within a group.
min(self, dim)Element-wise min over the specified dimension within a group.
nanmax(self, dim)Element-wise nanmax over the specified dimension within a group.
nanmin(self, dim)Element-wise nanmin over the specified dimension within a group.
nansum(self, dim)Element-wise nansum over the specified dimension within a group.
sum(self, dim)Element-wise sum over the specified dimension within a group.
- all(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise all over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the all.
- Returns:
The computed all over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[True, False, True, True]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').all('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[True, False, True, True]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).all('x') >>> grouped.sizes {'x': 2}
- any(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise any over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the any.
- Returns:
The computed any over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[True, False, True, True]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').any('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[True, False, True, True]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).any('x') >>> grouped.sizes {'x': 2}
- concat(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Concatenate bins within each group.
This operation is used with binned data to combine events from different bins that belong to the same group. The dimension being reduced (specified by the dim argument) is removed, and the events from all bins along that dimension within each group are concatenated.
- Parameters:
dim – Dimension to reduce by concatenating bins.
- Returns:
A DataArray or Dataset with concatenated binned data for each group.
Examples
Concatenate binned data within groups:
>>> import scipp as sc >>> table = sc.data.table_xyz(100) >>> binned = table.bin(x=4) >>> binned.coords['group'] = sc.array(dims=['x'], values=['A', 'B', 'A', 'B']) >>> grouped = binned.groupby('group').concat('x') >>> grouped.sizes {'group': 2}
The result contains binned data where events from bins 0 and 2 (group ‘A’) are concatenated into one bin, and events from bins 1 and 3 (group ‘B’) are concatenated into another bin.
- max(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise max over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the max.
- Returns:
The computed max over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').max('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).max('x') >>> grouped.sizes {'x': 2}
- mean(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise mean over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the mean.
- Returns:
The computed mean over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').mean('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).mean('x') >>> grouped.sizes {'x': 2}
- min(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise min over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the min.
- Returns:
The computed min over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').min('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).min('x') >>> grouped.sizes {'x': 2}
- nanmax(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise nanmax over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the nanmax.
- Returns:
The computed nanmax over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').nanmax('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).nanmax('x') >>> grouped.sizes {'x': 2}
- nanmin(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise nanmin over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the nanmin.
- Returns:
The computed nanmin over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').nanmin('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).nanmin('x') >>> grouped.sizes {'x': 2}
- nansum(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise nansum over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the nansum.
- Returns:
The computed nansum over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').nansum('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).nansum('x') >>> grouped.sizes {'x': 2}
- sum(self: scipp._scipp.core.GroupByDataArray, dim: str) scipp._scipp.core.DataArray#
Element-wise sum over the specified dimension within a group.
- Parameters:
dim (Dim) – Dimension to reduce when computing the sum.
- Returns:
The computed sum over each group, combined along the dimension specified when calling
scipp.groupby().- Return type:
Examples
Group by label coordinate:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'label': sc.array(dims=['x'], values=['a', 'b', 'a', 'b'])} ... ) >>> grouped = da.groupby('label').sum('x') >>> grouped.sizes {'label': 2}
Group by bin edges:
>>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0, 4.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5, 3.5])} ... ) >>> bins = sc.array(dims=['x'], values=[0.0, 2.0, 4.0]) >>> grouped = da.groupby('x', bins=bins).sum('x') >>> grouped.sizes {'x': 2}