Skip to content

Commit d933578

Browse files
spencerkclarkdcheriankeewis
authored
Port fix from pandas-dev/pandas#55283 to cftime resample (#8393)
* Port fix from pandas-dev/pandas#55283 to cftime resample [test-upstream] * Skip test for pandas versions older than 2.2 [test-upstream] * Update doc/whats-new.rst Co-authored-by: Justus Magin <[email protected]> --------- Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: Justus Magin <[email protected]>
1 parent fcdc810 commit d933578

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

doc/whats-new.rst

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ Deprecations
4343
Bug fixes
4444
~~~~~~~~~
4545

46+
- Port `bug fix from pandas <https://github.com/pandas-dev/pandas/pull/55283>`_
47+
to eliminate the adjustment of resample bin edges in the case that the
48+
resampling frequency has units of days and is greater than one day
49+
(e.g. ``"2D"``, ``"3D"`` etc.) and the ``closed`` argument is set to
50+
``"right"`` to xarray's implementation of resample for data indexed by a
51+
:py:class:`CFTimeIndex` (:pull:`8393`).
52+
By `Spencer Clark <https://github.com/spencerkclark>`_.
4653

4754
Documentation
4855
~~~~~~~~~~~~~

xarray/core/resample_cftime.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
from xarray.coding.cftime_offsets import (
4747
BaseCFTimeOffset,
48-
Day,
4948
MonthEnd,
5049
QuarterEnd,
5150
Tick,
@@ -254,8 +253,7 @@ def _adjust_bin_edges(
254253
labels: np.ndarray,
255254
):
256255
"""This is required for determining the bin edges resampling with
257-
daily frequencies greater than one day, month end, and year end
258-
frequencies.
256+
month end, quarter end, and year end frequencies.
259257
260258
Consider the following example. Let's say you want to downsample the
261259
time series with the following coordinates to month end frequency:
@@ -283,14 +281,8 @@ def _adjust_bin_edges(
283281
The labels are still:
284282
285283
CFTimeIndex([2000-01-31 00:00:00, 2000-02-29 00:00:00], dtype='object')
286-
287-
This is also required for daily frequencies longer than one day and
288-
year-end frequencies.
289284
"""
290-
is_super_daily = isinstance(freq, (MonthEnd, QuarterEnd, YearEnd)) or (
291-
isinstance(freq, Day) and freq.n > 1
292-
)
293-
if is_super_daily:
285+
if isinstance(freq, (MonthEnd, QuarterEnd, YearEnd)):
294286
if closed == "right":
295287
datetime_bins = datetime_bins + datetime.timedelta(days=1, microseconds=-1)
296288
if datetime_bins[-2] > index.max():

xarray/tests/test_cftimeindex_resample.py

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
import pandas as pd
88
import pytest
9+
from packaging.version import Version
910

1011
import xarray as xr
1112
from xarray.core.pdcompat import _convert_base_to_offset
@@ -122,6 +123,16 @@ def da(index) -> xr.DataArray:
122123
)
123124
def test_resample(freqs, closed, label, base, offset) -> None:
124125
initial_freq, resample_freq = freqs
126+
if (
127+
resample_freq == "4001D"
128+
and closed == "right"
129+
and Version(pd.__version__) < Version("2.2")
130+
):
131+
pytest.skip(
132+
"Pandas fixed a bug in this test case in version 2.2, which we "
133+
"ported to xarray, so this test no longer produces the same "
134+
"result as pandas for earlier pandas versions."
135+
)
125136
start = "2000-01-01T12:07:01"
126137
loffset = "12H"
127138
origin = "start"

0 commit comments

Comments
 (0)