🍾 Xarray is now 10 years old! πŸŽ‰

xarray.Dataset.groupby_bins

xarray.Dataset.groupby_bins#

Dataset.groupby_bins(group, bins, right=True, labels=None, precision=3, include_lowest=False, squeeze=None, restore_coord_dims=False)[source]#

Returns a DatasetGroupBy object for performing grouped operations.

Rather than using all unique values of group, the values are discretized first by applying pandas.cut [1] to group.

Parameters:
  • group (Hashable, DataArray or IndexVariable) – Array whose binned values should be used to group this array. If a string, must be the name of a variable contained in this dataset.

  • bins (int or array-like) – If bins is an int, it defines the number of equal-width bins in the range of x. However, in this case, the range of x is extended by .1% on each side to include the min or max values of x. If bins is a sequence it defines the bin edges allowing for non-uniform bin width. No extension of the range of x is done in this case.

  • right (bool, default: True) – Indicates whether the bins include the rightmost edge or not. If right == True (the default), then the bins [1,2,3,4] indicate (1,2], (2,3], (3,4].

  • labels (array-like or bool, default: None) – Used as labels for the resulting bins. Must be of the same length as the resulting bins. If False, string bin labels are assigned by pandas.cut.

  • precision (int, default: 3) – The precision at which to store and display the bins labels.

  • include_lowest (bool, default: False) – Whether the first interval should be left-inclusive or not.

  • squeeze (bool, default: True) – If β€œgroup” is a dimension of any arrays in this dataset, squeeze controls whether the subarrays have a dimension of length 1 along that dimension or if the dimension is squeezed out.

  • restore_coord_dims (bool, default: False) – If True, also restore the dimension order of multi-dimensional coordinates.

Returns:

grouped (DatasetGroupBy) – A DatasetGroupBy object patterned after pandas.GroupBy that can be iterated over in the form of (unique_value, grouped_array) pairs. The name of the group has the added suffix _bins in order to distinguish it from the original variable.

References