Skip to content

Implement weighted groupby #5480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 66 additions & 14 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
@@ -877,7 +877,22 @@ def reduce_array(ar):
return self.map(reduce_array, shortcut=shortcut)


class DatasetGroupBy(GroupBy, DatasetGroupbyArithmetic):
class DatasetGroupByCombine(GroupBy):
def _combine(self, applied):
"""Recombine the applied objects like the original."""
applied_example, applied = peek_at(applied)
coord, dim, positions = self._infer_concat_args(applied_example)
combined = concat(applied, dim)
combined = _maybe_reorder(combined, dim, positions)
# assign coord when the applied function does not return that coord
if coord is not None and dim not in applied_example.dims:
combined[coord.name] = coord
combined = self._maybe_restore_empty_groups(combined)
combined = self._maybe_unstack(combined)
return combined


class DatasetGroupBy(DatasetGroupByCombine, DatasetGroupbyArithmetic):
Copy link
Contributor Author

@dcherian dcherian Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled out combine because the inheritance order is DatasetGroupBy → DatasetGroupByArithmetic → ImplementsDatasetReduce → IncludeReduceMethods . That last one overwrites the reductions on WeighedGroupBy if it inherits from DatasetGroupBy


__slots__ = ()

@@ -931,19 +946,6 @@ def apply(self, func, args=(), shortcut=None, **kwargs):
)
return self.map(func, shortcut=shortcut, args=args, **kwargs)

def _combine(self, applied):
"""Recombine the applied objects like the original."""
applied_example, applied = peek_at(applied)
coord, dim, positions = self._infer_concat_args(applied_example)
combined = concat(applied, dim)
combined = _maybe_reorder(combined, dim, positions)
# assign coord when the applied function does not return that coord
if coord is not None and dim not in applied_example.dims:
combined[coord.name] = coord
combined = self._maybe_restore_empty_groups(combined)
combined = self._maybe_unstack(combined)
return combined

def reduce(self, func, dim=None, keep_attrs=None, **kwargs):
"""Reduce the items in this group by applying `func` along some
dimension(s).
@@ -994,3 +996,53 @@ def assign(self, **kwargs):
Dataset.assign
"""
return self.map(lambda ds: ds.assign(**kwargs))


class WeightedDatasetGroupBy(DatasetGroupByCombine):
def __init__(
self,
obj,
weights,
group,
squeeze=False,
restore_coord_dims=None,
):
# Only used for repr

weights_name = weights.name
if weights_name:
name_str = f"by {weights_name!r}"
else:
name_str = ""
weight_dims = ", ".join(weights.dims)

self.weights_repr = f"\nweighted along dimensions: {weight_dims} " f"{name_str}"
super().__init__(
# assigning as coords means weights get sliced out like
# groups
obj=obj.assign_coords({"__weights__": weights}),
group=group,
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)

def __repr__(self):
return f"{super().__repr__()}" + self.weights_repr

def _reduce(self, func, dim=None, skipna=None, keep_attrs=None):
if dim is None:
dim = self._group_dim

applied = (
getattr(ds.weighted(ds.__weights__), func)(
dim=dim, skipna=skipna, keep_attrs=keep_attrs
)
for ds in super()._iter_grouped()
)
return super()._combine(applied)

def mean(self):
return self._reduce("mean")

def sum(self):
return self._reduce("sum")
19 changes: 19 additions & 0 deletions xarray/core/weighted.py
Original file line number Diff line number Diff line change
@@ -263,6 +263,25 @@ def _implementation(self, func, dim, **kwargs) -> "Dataset":

return self.obj.map(func, dim=dim, **kwargs)

def groupby(
self,
group,
squeeze=False,
grouper=None,
bins=None,
restore_coord_dims=None,
cut_kwargs=None,
):
from .groupby import WeightedDatasetGroupBy

return WeightedDatasetGroupBy(
weights=self.weights,
obj=self.obj,
group=group,
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)


def _inject_docstring(cls, cls_name):