Skip to content

Commit

Permalink
Fix resampling codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Oct 29, 2022
1 parent 7ca884f commit 1891fdb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ def _get_index_and_items(index, grouper):
# This way we generate codes for the final output index: full_index.
# So for _flox_reduce we avoid one reindex and copy by avoiding
# _maybe_restore_empty_groups
codes = first_items.index.searchsorted(index, side=grouper.closed)
codes = (
first_items.index.searchsorted(
index, side="right" if grouper.closed == "left" else "left"
)
- 1
)
_apply_loffset(grouper, first_items)
full_index = first_items.index
if first_items.isnull().any():
Expand Down
7 changes: 6 additions & 1 deletion xarray/core/resample_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ def first_items(self, index):
datetime_bins, labels = _get_time_bins(
index, self.freq, self.closed, self.label, self.base
)
codes = np.searchsorted(labels, index, side=self.closed)
codes = (
np.searchsorted(
labels, index, side="right" if self.closed == "left" else "left"
)
- 1
)
if self.loffset is not None:
if isinstance(self.loffset, datetime.timedelta):
labels = labels + self.loffset
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,10 @@ def test_resample(self):
actual = array.resample(time="24H").reduce(np.mean)
assert_identical(expected, actual)

actual = array.resample(time="24H", closed="right").mean()
expected = DataArray(array.to_series().resample("24H", closed="right").mean())
assert_identical(expected, actual)

# Our use of `loffset` may change if we align our API with pandas' changes.
# ref https://github.com/pydata/xarray/pull/4537
actual = array.resample(time="24H", loffset="-12H").mean()
Expand Down

0 comments on commit 1891fdb

Please sign in to comment.