Skip to content
forked from pydata/xarray

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Mar 30, 2023
1 parent c905b74 commit 22ad7fa
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 104 deletions.
77 changes: 73 additions & 4 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,74 @@ def rolling_exp(

return rolling_exp.RollingExp(self, window, window_type)

def _groupby(self, groupby_cls, group, squeeze: bool, restore_coord_dims):
from xarray.core.groupby import UniqueGrouper, _validate_group

# While we don't generally check the type of every arg, passing
# multiple dimensions as multiple arguments is common enough, and the
# consequences hidden enough (strings evaluate as true) to warrant
# checking here.
# A future version could make squeeze kwarg only, but would face
# backward-compat issues.
if not isinstance(squeeze, bool):
raise TypeError(
f"`squeeze` must be True or False, but {squeeze} was supplied"
)

newobj, name = _validate_group(self, group)

grouper = UniqueGrouper()
return groupby_cls(
newobj,
{name: grouper},
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)

def _groupby_bins(
self,
groupby_cls,
group: Hashable | DataArray | IndexVariable,
bins: ArrayLike,
right: bool = True,
labels: ArrayLike | None = None,
precision: int = 3,
include_lowest: bool = False,
squeeze: bool = True,
restore_coord_dims: bool = False,
):
from xarray.core.groupby import BinGrouper, _validate_group

# While we don't generally check the type of every arg, passing
# multiple dimensions as multiple arguments is common enough, and the
# consequences hidden enough (strings evaluate as true) to warrant
# checking here.
# A future version could make squeeze kwarg only, but would face
# backward-compat issues.
if not isinstance(squeeze, bool):
raise TypeError(
f"`squeeze` must be True or False, but {squeeze} was supplied"
)

newobj, name = _validate_group(self, group)

grouper = BinGrouper(
bins=bins,
cut_kwargs={
"right": right,
"labels": labels,
"precision": precision,
"include_lowest": include_lowest,
},
)

return groupby_cls(
newobj,
{name: grouper},
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)

def _resample(
self,
resample_cls: type[T_Resample],
Expand Down Expand Up @@ -1000,12 +1068,13 @@ def _resample(
if base is not None:
offset = _convert_base_to_offset(base, freq, index)

name = RESAMPLE_DIM
group = DataArray(
dim_coord, coords=dim_coord.coords, dims=dim_coord.dims, name=RESAMPLE_DIM
dim_coord, coords=dim_coord.coords, dims=dim_coord.dims, name=name
)
newobj = self.copy().assign_coords({name: group})

grouper = TimeResampleGrouper(
group=group,
freq=freq,
closed=closed,
label=label,
Expand All @@ -1015,8 +1084,8 @@ def _resample(
)

return resample_cls(
self,
grouper=grouper,
newobj,
{name: grouper},
dim=dim_name,
resample_dim=RESAMPLE_DIM,
restore_coord_dims=restore_coord_dims,
Expand Down
52 changes: 13 additions & 39 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6256,22 +6256,13 @@ def groupby(
core.groupby.DataArrayGroupBy
pandas.DataFrame.groupby
"""
from xarray.core.groupby import DataArrayGroupBy, UniqueGrouper

# While we don't generally check the type of every arg, passing
# multiple dimensions as multiple arguments is common enough, and the
# consequences hidden enough (strings evaluate as true) to warrant
# checking here.
# A future version could make squeeze kwarg only, but would face
# backward-compat issues.
if not isinstance(squeeze, bool):
raise TypeError(
f"`squeeze` must be True or False, but {squeeze} was supplied"
)
from xarray.core.groupby import DataArrayGroupBy

grouper = UniqueGrouper(group)
return DataArrayGroupBy(
self, grouper, squeeze=squeeze, restore_coord_dims=restore_coord_dims
return self._groupby(
groupby_cls=DataArrayGroupBy,
group=group,
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)

def groupby_bins(
Expand Down Expand Up @@ -6342,33 +6333,16 @@ def groupby_bins(
----------
.. [1] http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html
"""
from xarray.core.groupby import BinGrouper, DataArrayGroupBy

# While we don't generally check the type of every arg, passing
# multiple dimensions as multiple arguments is common enough, and the
# consequences hidden enough (strings evaluate as true) to warrant
# checking here.
# A future version could make squeeze kwarg only, but would face
# backward-compat issues.
if not isinstance(squeeze, bool):
raise TypeError(
f"`squeeze` must be True or False, but {squeeze} was supplied"
)
from xarray.core.groupby import DataArrayGroupBy

grouper = BinGrouper(
return self._groupby_bins(
groupby_cls=DataArrayGroupBy,
group=group,
bins=bins,
cut_kwargs={
"right": right,
"labels": labels,
"precision": precision,
"include_lowest": include_lowest,
},
)

return DataArrayGroupBy(
self,
grouper,
right=right,
labels=labels,
precision=precision,
include_lowest=include_lowest,
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)
Expand Down
43 changes: 15 additions & 28 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8943,23 +8943,13 @@ def groupby(
Dataset.resample
DataArray.resample
"""
from xarray.core.groupby import DatasetGroupBy, UniqueGrouper

# While we don't generally check the type of every arg, passing
# multiple dimensions as multiple arguments is common enough, and the
# consequences hidden enough (strings evaluate as true) to warrant
# checking here.
# A future version could make squeeze kwarg only, but would face
# backward-compat issues.
if not isinstance(squeeze, bool):
raise TypeError(
f"`squeeze` must be True or False, but {squeeze} was supplied"
)
from xarray.core.groupby import DatasetGroupBy

grouper = UniqueGrouper(group)

return DatasetGroupBy(
self, grouper, squeeze=squeeze, restore_coord_dims=restore_coord_dims
return self._groupby(
groupby_cls=DatasetGroupBy,
group=group,
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)

def groupby_bins(
Expand Down Expand Up @@ -9030,21 +9020,18 @@ def groupby_bins(
----------
.. [1] http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html
"""
from xarray.core.groupby import BinGrouper, DatasetGroupBy
from xarray.core.groupby import DatasetGroupBy

grouper = BinGrouper(
return self._groupby_bins(
groupby_cls=DatasetGroupBy,
group=group,
bins=bins,
cut_kwargs={
"right": right,
"labels": labels,
"precision": precision,
"include_lowest": include_lowest,
},
)

return DatasetGroupBy(
self, grouper, squeeze=squeeze, restore_coord_dims=restore_coord_dims
right=right,
labels=labels,
precision=precision,
include_lowest=include_lowest,
squeeze=squeeze,
restore_coord_dims=restore_coord_dims,
)

def weighted(self, weights: DataArray) -> DatasetWeighted:
Expand Down
Loading

0 comments on commit 22ad7fa

Please sign in to comment.