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

Port fix from pandas-dev/pandas#55283 to cftime resample #8393

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>`_.
dcherian marked this conversation as resolved.
Show resolved Hide resolved

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