Skip to content

Commit

Permalink
Port fix from pandas-dev/pandas#55283 to cftime resample (#8393)
Browse files Browse the repository at this point in the history
* 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 <keewis@users.noreply.github.com>

---------

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent fcdc810 commit d933578
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 7 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Deprecations
Bug fixes
~~~~~~~~~

- Port `bug fix from pandas <https://github.com/pandas-dev/pandas/pull/55283>`_
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 <https://github.com/spencerkclark>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
12 changes: 2 additions & 10 deletions xarray/core/resample_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

from xarray.coding.cftime_offsets import (
BaseCFTimeOffset,
Day,
MonthEnd,
QuarterEnd,
Tick,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_cftimeindex_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit d933578

Please sign in to comment.