Skip to content

Commit

Permalink
Refactor Grouper objects (#8776)
Browse files Browse the repository at this point in the history
* Refactor resampling.

1. Rename to Resampler from ResampleGrouper
2. Move code from common.resample to TimeResampler

* Single Grouper class

* Fixes

* Fixes

* Add comments.

* Apply suggestions from code review

Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>

* Review feedback

* fix

---------

Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>
  • Loading branch information
dcherian and Illviljan authored Mar 7, 2024
1 parent cb09522 commit 8468be0
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 152 deletions.
42 changes: 10 additions & 32 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from xarray.core.utils import (
Frozen,
either_dict_or_kwargs,
emit_user_level_warning,
is_scalar,
)
from xarray.namedarray.core import _raise_if_any_duplicate_dimensions
Expand Down Expand Up @@ -1050,8 +1049,7 @@ def _resample(
# TODO support non-string indexer after removing the old API.

from xarray.core.dataarray import DataArray
from xarray.core.groupby import ResolvedTimeResampleGrouper, TimeResampleGrouper
from xarray.core.pdcompat import _convert_base_to_offset
from xarray.core.groupby import ResolvedGrouper, TimeResampler
from xarray.core.resample import RESAMPLE_DIM

# note: the second argument (now 'skipna') use to be 'dim'
Expand All @@ -1074,44 +1072,24 @@ def _resample(
dim_name: Hashable = dim
dim_coord = self[dim]

if loffset is not None:
emit_user_level_warning(
"Following pandas, the `loffset` parameter to resample is deprecated. "
"Switch to updating the resampled dataset time coordinate using "
"time offset arithmetic. For example:\n"
" >>> offset = pd.tseries.frequencies.to_offset(freq) / 2\n"
' >>> resampled_ds["time"] = resampled_ds.get_index("time") + offset',
FutureWarning,
)

if base is not None:
emit_user_level_warning(
"Following pandas, the `base` parameter to resample will be deprecated in "
"a future version of xarray. Switch to using `origin` or `offset` instead.",
FutureWarning,
)

if base is not None and offset is not None:
raise ValueError("base and offset cannot be present at the same time")

if base is not None:
index = self._indexes[dim_name].to_pandas_index()
offset = _convert_base_to_offset(base, freq, index)
group = DataArray(
dim_coord,
coords=dim_coord.coords,
dims=dim_coord.dims,
name=RESAMPLE_DIM,
)

grouper = TimeResampleGrouper(
grouper = TimeResampler(
freq=freq,
closed=closed,
label=label,
origin=origin,
offset=offset,
loffset=loffset,
base=base,
)

group = DataArray(
dim_coord, coords=dim_coord.coords, dims=dim_coord.dims, name=RESAMPLE_DIM
)

rgrouper = ResolvedTimeResampleGrouper(grouper, group, self)
rgrouper = ResolvedGrouper(grouper, group, self)

return resample_cls(
self,
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6704,13 +6704,13 @@ def groupby(
"""
from xarray.core.groupby import (
DataArrayGroupBy,
ResolvedUniqueGrouper,
ResolvedGrouper,
UniqueGrouper,
_validate_groupby_squeeze,
)

_validate_groupby_squeeze(squeeze)
rgrouper = ResolvedUniqueGrouper(UniqueGrouper(), group, self)
rgrouper = ResolvedGrouper(UniqueGrouper(), group, self)
return DataArrayGroupBy(
self,
(rgrouper,),
Expand Down Expand Up @@ -6789,7 +6789,7 @@ def groupby_bins(
from xarray.core.groupby import (
BinGrouper,
DataArrayGroupBy,
ResolvedBinGrouper,
ResolvedGrouper,
_validate_groupby_squeeze,
)

Expand All @@ -6803,7 +6803,7 @@ def groupby_bins(
"include_lowest": include_lowest,
},
)
rgrouper = ResolvedBinGrouper(grouper, group, self)
rgrouper = ResolvedGrouper(grouper, group, self)

return DataArrayGroupBy(
self,
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10179,13 +10179,13 @@ def groupby(
"""
from xarray.core.groupby import (
DatasetGroupBy,
ResolvedUniqueGrouper,
ResolvedGrouper,
UniqueGrouper,
_validate_groupby_squeeze,
)

_validate_groupby_squeeze(squeeze)
rgrouper = ResolvedUniqueGrouper(UniqueGrouper(), group, self)
rgrouper = ResolvedGrouper(UniqueGrouper(), group, self)

return DatasetGroupBy(
self,
Expand Down Expand Up @@ -10265,7 +10265,7 @@ def groupby_bins(
from xarray.core.groupby import (
BinGrouper,
DatasetGroupBy,
ResolvedBinGrouper,
ResolvedGrouper,
_validate_groupby_squeeze,
)

Expand All @@ -10279,7 +10279,7 @@ def groupby_bins(
"include_lowest": include_lowest,
},
)
rgrouper = ResolvedBinGrouper(grouper, group, self)
rgrouper = ResolvedGrouper(grouper, group, self)

return DatasetGroupBy(
self,
Expand Down
Loading

0 comments on commit 8468be0

Please sign in to comment.