From 272c68967b024b043504bcb1a9c168b6d68e8b98 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Mon, 30 Oct 2023 18:24:50 -0400 Subject: [PATCH 1/3] Port fix from pandas-dev/pandas#55283 to cftime resample [test-upstream] --- doc/whats-new.rst | 7 +++++++ xarray/core/resample_cftime.py | 12 ++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b24a19c9129..18c0f3fc3f1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -43,6 +43,13 @@ Deprecations Bug fixes ~~~~~~~~~ +- Port `bug fix from pandas `_ + to eliminate the adjustment of resample bin edges in the case that the + resampling frequency has units of days and is greater than one day + (e.g. ``"2D"``, ``"3D"`` etc.) and the ``closed`` argument is set to + ``"right"`` to xarray's implementation of resample for data indexed by a + :py:class:`CFTimeIndex` (:pull:`8393`). By `Spencer Clark + `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/resample_cftime.py b/xarray/core/resample_cftime.py index 43edbc08456..5e3f2f57397 100644 --- a/xarray/core/resample_cftime.py +++ b/xarray/core/resample_cftime.py @@ -45,7 +45,6 @@ from xarray.coding.cftime_offsets import ( BaseCFTimeOffset, - Day, MonthEnd, QuarterEnd, Tick, @@ -254,8 +253,7 @@ def _adjust_bin_edges( labels: np.ndarray, ): """This is required for determining the bin edges resampling with - daily frequencies greater than one day, month end, and year end - frequencies. + month end, quarter end, and year end frequencies. Consider the following example. Let's say you want to downsample the time series with the following coordinates to month end frequency: @@ -283,14 +281,8 @@ def _adjust_bin_edges( The labels are still: CFTimeIndex([2000-01-31 00:00:00, 2000-02-29 00:00:00], dtype='object') - - This is also required for daily frequencies longer than one day and - year-end frequencies. """ - is_super_daily = isinstance(freq, (MonthEnd, QuarterEnd, YearEnd)) or ( - isinstance(freq, Day) and freq.n > 1 - ) - if is_super_daily: + if isinstance(freq, (MonthEnd, QuarterEnd, YearEnd)): if closed == "right": datetime_bins = datetime_bins + datetime.timedelta(days=1, microseconds=-1) if datetime_bins[-2] > index.max(): From d25d14b93f0be07d8a3610cdaf865b2c36fffa2e Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Tue, 31 Oct 2023 07:42:09 -0400 Subject: [PATCH 2/3] Skip test for pandas versions older than 2.2 [test-upstream] --- xarray/tests/test_cftimeindex_resample.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/xarray/tests/test_cftimeindex_resample.py b/xarray/tests/test_cftimeindex_resample.py index 72aaae595de..f8e6e80452a 100644 --- a/xarray/tests/test_cftimeindex_resample.py +++ b/xarray/tests/test_cftimeindex_resample.py @@ -6,6 +6,7 @@ import numpy as np import pandas as pd import pytest +from packaging.version import Version import xarray as xr from xarray.core.pdcompat import _convert_base_to_offset @@ -122,6 +123,16 @@ def da(index) -> xr.DataArray: ) def test_resample(freqs, closed, label, base, offset) -> None: initial_freq, resample_freq = freqs + if ( + resample_freq == "4001D" + and closed == "right" + and Version(pd.__version__) < Version("2.2") + ): + pytest.skip( + "Pandas fixed a bug in this test case in version 2.2, which we " + "ported to xarray, so this test no longer produces the same " + "result as pandas for earlier pandas versions." + ) start = "2000-01-01T12:07:01" loffset = "12H" origin = "start" From 264c41108ffe770bb5a182e2f6c882596fb9cdad Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Tue, 31 Oct 2023 07:45:32 -0600 Subject: [PATCH 3/3] Update doc/whats-new.rst Co-authored-by: Justus Magin --- doc/whats-new.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 18c0f3fc3f1..3d9d3f6f310 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -48,8 +48,8 @@ Bug fixes resampling frequency has units of days and is greater than one day (e.g. ``"2D"``, ``"3D"`` etc.) and the ``closed`` argument is set to ``"right"`` to xarray's implementation of resample for data indexed by a - :py:class:`CFTimeIndex` (:pull:`8393`). By `Spencer Clark - `_. + :py:class:`CFTimeIndex` (:pull:`8393`). + By `Spencer Clark `_. Documentation ~~~~~~~~~~~~~