Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for date offset strings with resample loffset #8422

Merged
merged 3 commits into from Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
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
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
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