Skip to content

Commit 1891fdb

Browse files
committed
Fix resampling codes.
1 parent 7ca884f commit 1891fdb

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

xarray/core/groupby.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,12 @@ def _get_index_and_items(index, grouper):
292292
# This way we generate codes for the final output index: full_index.
293293
# So for _flox_reduce we avoid one reindex and copy by avoiding
294294
# _maybe_restore_empty_groups
295-
codes = first_items.index.searchsorted(index, side=grouper.closed)
295+
codes = (
296+
first_items.index.searchsorted(
297+
index, side="right" if grouper.closed == "left" else "left"
298+
)
299+
- 1
300+
)
296301
_apply_loffset(grouper, first_items)
297302
full_index = first_items.index
298303
if first_items.isnull().any():

xarray/core/resample_cftime.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ def first_items(self, index):
9191
datetime_bins, labels = _get_time_bins(
9292
index, self.freq, self.closed, self.label, self.base
9393
)
94-
codes = np.searchsorted(labels, index, side=self.closed)
94+
codes = (
95+
np.searchsorted(
96+
labels, index, side="right" if self.closed == "left" else "left"
97+
)
98+
- 1
99+
)
95100
if self.loffset is not None:
96101
if isinstance(self.loffset, datetime.timedelta):
97102
labels = labels + self.loffset

xarray/tests/test_groupby.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,10 @@ def test_resample(self):
14801480
actual = array.resample(time="24H").reduce(np.mean)
14811481
assert_identical(expected, actual)
14821482

1483+
actual = array.resample(time="24H", closed="right").mean()
1484+
expected = DataArray(array.to_series().resample("24H", closed="right").mean())
1485+
assert_identical(expected, actual)
1486+
14831487
# Our use of `loffset` may change if we align our API with pandas' changes.
14841488
# ref https://github.com/pydata/xarray/pull/4537
14851489
actual = array.resample(time="24H", loffset="-12H").mean()

0 commit comments

Comments
 (0)