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

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

Open
5 tasks done
awikner opened this issue Apr 13, 2024 · 2 comments · May be fixed by #8942
Open
5 tasks done

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

awikner opened this issue Apr 13, 2024 · 2 comments · May be fixed by #8942

Comments

@awikner
Copy link

awikner commented Apr 13, 2024

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
@awikner awikner added bug needs triage Issue that has not been reviewed by xarray team member labels Apr 13, 2024
Copy link

welcome bot commented Apr 13, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@spencerkclark spencerkclark linked a pull request Apr 14, 2024 that will close this issue
8 tasks
@spencerkclark
Copy link
Member

Indeed for the time being xarray only supports the calendar-specific cftime.datetime subclasses like cftime.DatetimeProlepticGregorian. I started a PR to enable support for calendar-aware cftime.datetime instances in #8942. In principle it should not be too hard, though it will take some time to make sure everything is tested thoroughly enough.

@kmuehlbauer kmuehlbauer added topic-cftime and removed needs triage Issue that has not been reviewed by xarray team member labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants