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 negative frequency fix for pandas.date_range to cftime_range #8999

Merged
merged 1 commit into from
May 6, 2024

Conversation

spencerkclark
Copy link
Member

@spencerkclark spencerkclark commented May 4, 2024

Like pandas.date_range, cftime_range would previously return dates outside the range of the specified start and end dates if provided a negative frequency:

>>> start = cftime.DatetimeGregorian(2023, 10, 31)
>>> end = cftime.DatetimeGregorian(2021, 11, 1)
>>> xr.cftime_range(start, end, freq="-1YE")
CFTimeIndex([2023-12-31 00:00:00, 2022-12-31 00:00:00, 2021-12-31 00:00:00],
            dtype='object', length=3, calendar='standard', freq='-1YE-DEC')

This PR ports a bug fix from pandas (pandas-dev/pandas#56147) to prevent this from happening. The above example now produces:

>>> start = cftime.DatetimeGregorian(2023, 10, 31)
>>> end = cftime.DatetimeGregorian(2021, 11, 1)
>>> xr.cftime_range(start, end, freq="-1YE")
CFTimeIndex([2022-12-31 00:00:00, 2021-12-31 00:00:00],
            dtype='object', length=2, calendar='standard', freq=None)

Since this is a bug fix, we do not make any attempt to preserve the old behavior if an earlier version of pandas is installed. In the testing context this means we skip some tests for pandas versions less than 3.0.

@spencerkclark spencerkclark added the run-upstream Run upstream CI label May 4, 2024
@spencerkclark spencerkclark added run-upstream Run upstream CI and removed run-upstream Run upstream CI labels May 4, 2024
@dcherian
Copy link
Contributor

dcherian commented May 6, 2024

Thanks!

@dcherian dcherian merged commit e0f2cee into pydata:main May 6, 2024
26 of 31 checks passed
@spencerkclark spencerkclark deleted the fix-negative-frequency-bug branch May 7, 2024 12:37
andersy005 added a commit that referenced this pull request May 10, 2024
* main:
  Avoid auto creation of indexes in concat (#8872)
  Fix benchmark CI (#9013)
  Avoid extra read from disk when creating Pandas Index. (#8893)
  Add a benchmark to monitor performance for large dataset indexing (#9012)
  Zarr: Optimize `region="auto"` detection (#8997)
  Trigger CI only if code files are modified. (#9006)
  Fix for ruff 0.4.3 (#9007)
  Port negative frequency fix for `pandas.date_range` to `cftime_range` (#8999)
  Bump codecov/codecov-action from 4.3.0 to 4.3.1 in the actions group (#9004)
  Speed up localize (#8536)
  Simplify fast path (#9001)
  Add argument check_dims to assert_allclose to allow transposed inputs (#5733) (#8991)
  Fix syntax error in test related to cupy (#9000)
andersy005 added a commit to hmaarrfk/xarray that referenced this pull request May 10, 2024
* backend-indexing:
  Trigger CI only if code files are modified. (pydata#9006)
  Enable explicit use of key tuples (instead of *Indexer objects) in indexing adapters and explicitly indexed arrays (pydata#8870)
  add `.oindex` and `.vindex` to `BackendArray` (pydata#8885)
  temporary enable CI triggers on feature branch
  Avoid auto creation of indexes in concat (pydata#8872)
  Fix benchmark CI (pydata#9013)
  Avoid extra read from disk when creating Pandas Index. (pydata#8893)
  Add a benchmark to monitor performance for large dataset indexing (pydata#9012)
  Zarr: Optimize `region="auto"` detection (pydata#8997)
  Trigger CI only if code files are modified. (pydata#9006)
  Fix for ruff 0.4.3 (pydata#9007)
  Port negative frequency fix for `pandas.date_range` to `cftime_range` (pydata#8999)
  Bump codecov/codecov-action from 4.3.0 to 4.3.1 in the actions group (pydata#9004)
  Speed up localize (pydata#8536)
  Simplify fast path (pydata#9001)
  Add argument check_dims to assert_allclose to allow transposed inputs (pydata#5733) (pydata#8991)
  Fix syntax error in test related to cupy (pydata#9000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-upstream Run upstream CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants