diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 3a9be494db2..b6bad62dd7c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -50,6 +50,9 @@ Bug fixes ``"right"`` to xarray's implementation of resample for data indexed by a :py:class:`CFTimeIndex` (:pull:`8393`). By `Spencer Clark `_. +- Fix to once again support date offset strings as input to the loffset + parameter of resample and test this functionality (:pull:`8422`, :issue:`8399`). + By `Katelyn FitzGerald `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/resample_cftime.py b/xarray/core/resample_cftime.py index 5e3f2f57397..7241faa1c61 100644 --- a/xarray/core/resample_cftime.py +++ b/xarray/core/resample_cftime.py @@ -151,7 +151,10 @@ def first_items(self, index: CFTimeIndex): f"Got {self.loffset}." ) - labels = labels + pd.to_timedelta(self.loffset) + if isinstance(self.loffset, datetime.timedelta): + labels = labels + self.loffset + else: + labels = labels + to_offset(self.loffset) # check binner fits data if index[0] < datetime_bins[0]: diff --git a/xarray/tests/test_cftimeindex_resample.py b/xarray/tests/test_cftimeindex_resample.py index f8e6e80452a..284460c3686 100644 --- a/xarray/tests/test_cftimeindex_resample.py +++ b/xarray/tests/test_cftimeindex_resample.py @@ -260,7 +260,7 @@ def test_timedelta_offset() -> None: xr.testing.assert_identical(timedelta_result, string_result) -@pytest.mark.parametrize("loffset", ["12H", datetime.timedelta(hours=-12)]) +@pytest.mark.parametrize("loffset", ["MS", "12H", datetime.timedelta(hours=-12)]) def test_resample_loffset_cftimeindex(loffset) -> None: datetimeindex = pd.date_range("2000-01-01", freq="6H", periods=10) da_datetimeindex = xr.DataArray(np.arange(10), [("time", datetimeindex)])