pydata / pydata/xarray

xr.cftime_range fails when using proleptic_gregorian calendar in cftime.datetime object

Open
#8,941 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-cftime
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

I want to use the xarray.cftime_range function to get a set of datetimes between two cftime.datetime objects that use the proleptic_gregorian calendar. However, when I use this function with this calendar, I receive the following error:

File "/Users/Alexander/miniconda3/envs/xrtest/lib/python3.11/site-packages/xarray/core/resample_cftime.py", line 491, in exact_cftime_datetime_difference
    seconds = b.replace(microsecond=0) - a.replace(microsecond=0)
              ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
  File "src/cftime/_cftime.pyx", line 1574, in cftime._cftime.datetime.__sub__
TypeError: cannot compute the time difference between dates with different calendars
What did you expect to happen?

I expect this function to return a list of dates between the two dates I selected, with each date spaced by one day. In the minimal example, I provide the expected output obtained when I instead use the cftime.DatetimeProlepticGregorian object.

Minimal Complete Verifiable Example
#xarray==2024.3.0
#cftime==1.6.3
import xarray as xr
import cftime

start_date = cftime.DatetimeProlepticGregorian(1,1,1)
end_date = cftime.DatetimeProlepticGregorian(2,1,1)

daterange = xr.cftime_range(start_date, end_date, calendar='proleptic_gregorian')
print(daterange)

start_date = cftime.datetime(1,1,1,calendar = 'proleptic_gregorian')
end_date = cftime.datetime(2,1,1,calendar = 'proleptic_gregorian')

daterange = xr.cftime_range(start_date, end_date, calendar='proleptic_gregorian')
print(daterange)
MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

No response

Anything else we need to know?

No response

Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.11.8 | packaged by conda-forge | (main, Feb 16 2024, 20:49:36) [Clang 16.0.6 ] python-bits: 64 OS: Darwin OS-release: 23.4.0 machine: arm64 processor: arm byteorder: little LC_ALL: None LANG: None LOCALE: ('en_US', 'UTF-8') libhdf5: None libnetcdf: None xarray: 2024.3.0 pandas: 2.2.2 numpy: 1.26.4 scipy: None netCDF4: None pydap: None h5netcdf: None h5py: None Nio: None zarr: None cftime: 1.6.3 nc_time_axis: None iris: None bottleneck: None dask: None distributed: None matplotlib: None cartopy: None seaborn: None numbagg: None fsspec: None cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 69.2.0 pip: 24.0 conda: None pytest: None mypy: None IPython: None sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in xarray/core/resample_cftime.py at exact_cftime_datetime_difference and reproduce the cftime_range example with both kinds of proleptic_gregorian cftime.datetime objects. Trace why the subtraction reports different calendars, then verify that xr.cftime_range returns dates spaced one day apart for the provided inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.