Skip to content

Following pandas 3.0, make Day cftime offset non-Tick-like #10650

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

spencerkclark
Copy link
Member

In pandas 3.0, Day will no longer be considered a Tick-like frequency (pandas-dev/pandas#61985). This PR ports this change to the cftime version of this offset. The main implication in the cftime / xarray context is that it means the offset and origin options in resample will no longer have an effect when resampling to a daily frequency. As in pandas-dev/pandas#62101, warnings will be emitted if non-default values are passed.

@spencerkclark spencerkclark added run-upstream Run upstream CI and removed topic-cftime labels Aug 17, 2025
@spencerkclark spencerkclark force-pushed the pandas-like/port-Day-offset-changes branch from 2f3d6c0 to 6b7f4bd Compare August 17, 2025 14:50
@spencerkclark spencerkclark force-pushed the pandas-like/port-Day-offset-changes branch from 6b7f4bd to ea43798 Compare August 17, 2025 14:52
initial_freq, resample_freq = freqs
if (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since our minimum version of pandas is greater than or equal to 2.2, I took the opportunity to remove this old test-skipping logic.

)
@pytest.mark.parametrize("closed", [None, "left", "right"])
@pytest.mark.parametrize("label", [None, "left", "right"])
def test_resample_with_non_tick_resample_freq(freqs, closed, label) -> None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the offset and origin options are not relevant when resampling to a non-Tick frequency, I split these tests off into their function to avoid warnings and avoid unnecessary duplication of tests.

@@ -830,7 +830,6 @@ def test_cftimeindex_add_timedeltaindex(calendar) -> None:
@pytest.mark.parametrize(
"freq,units",
[
("D", "D"),
Copy link
Member Author

@spencerkclark spencerkclark Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that Day is non-Tick-like, multiplying by a float is no longer valid in pandas, so shift with a float value and Day offset will raise an error:

>>> import pandas as pd
>>> times = pd.date_range("2000", periods=5)
>>> times.shift(0.5, "D")
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    times.shift(0.5, "D")
    ~~~~~~~~~~~^^^^^^^^^^
  File "/Users/spencer/software/pandas/pandas/core/indexes/datetimelike.py", line 514, in shift
    start = self[0] + periods * self.freq
                      ~~~~~~~~^~~~~~~~~~~
TypeError: unsupported operand type(s) for *: 'float' and 'pandas._libs.tslibs.offsets.Day'

A similar error is raised now for attempting to shift a CFTimeIndex in this way. This is tested in b87bb35.

@@ -228,7 +276,7 @@ def test_invalid_offset_error(offset: str | int) -> None:
cftime_index = xr.date_range("2000", periods=5, use_cftime=True)
da_cftime = da(cftime_index)
with pytest.raises(ValueError, match="offset must be"):
da_cftime.resample(time="2D", offset=offset) # type: ignore[arg-type]
da_cftime.resample(time="2h", offset=offset) # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to "2h", since "2D" is no longer a Tick-like frequency.

Comment on lines +289 to +290
timedelta_result = da_cftime.resample(time="2h", offset=timedelta).mean()
string_result = da_cftime.resample(time="2h", offset=string).mean()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to "2h", since "2D" is no longer a Tick-like frequency.

@@ -445,7 +445,6 @@ def test_eq(a, b):
(Second(), 3, Second(n=3)),
(Millisecond(), 3, Millisecond(n=3)),
(Microsecond(), 3, Microsecond(n=3)),
(Day(), 0.5, Hour(n=12)),
Copy link
Member Author

@spencerkclark spencerkclark Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that Day is non-Tick-like, multiplying by a float is no longer valid in pandas:

>>> import pandas as pd
>>> 2.0 * pd.tseries.frequencies.Day(2)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    2.0 * pd.tseries.frequencies.Day(2)
    ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for *: 'float' and 'pandas._libs.tslibs.offsets.Day'

A similar error is raised now for the cftime version of this offset. This is tested in b87bb35.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

⚠️ Nightly upstream-dev CI failed ⚠️
1 participant