Skip to content

Commit

Permalink
Fix for date offset strings with resample loffset (#8422)
Browse files Browse the repository at this point in the history
* change back to using to_offset in first_items

* add date offset to loffset test

* add to release notes
  • Loading branch information
kafitzgerald authored Nov 8, 2023
1 parent 3732584 commit feba698
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/spencerkclark>`_.
- 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 <https://github.com/kafitzgerald>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
5 changes: 4 additions & 1 deletion xarray/core/resample_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_cftimeindex_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down

0 comments on commit feba698

Please sign in to comment.