shnitsel.core.xrhelpers.mgroupby

mgroupby(obj, levels)

Group a Dataset or DataArray by several levels of a MultiIndex it contains.

Parameters:
  • obj (Dataset | DataArray) – The xr object to group

  • levels (Sequence[str]) – Names of MultiIndex levels all belonging to the same MultiIndex

Returns:

  • The grouped object, which behaves as documented at xr.Dataset.groupby()

  • and xr.DataArray.groupby with the caveat that the specified levels have been

  • ”flattened” into a single Multiindex level of tuples.

Raises:

ValueError – If no MultiIndex is found, or if the named levels belong to different MultiIndexes.

Return type:

DataArrayGroupBy | DatasetGroupBy

Warning

The function does not currently check whether the levels specified are really levels of a MultiIndex, as opposed to names of non-MultiIndex indexes.