pydata / pydata/xarray

More robust checks for cftime input to xr.date_range

Open
#10,065 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What is your issue?

I recently ran into a case where the logic in xr.date_range isn't quite robust enough (detail below).

This is something we use in a downstream package and while I can definitely add some additional checks there I figured I'd log an issue here and see if you all might be open to addressing this in Xarray. Happy to work on a PR if that's helpful (though I'm not terribly familiar with the code).

The following example should replicate the failure:

import xarray as xr

times = xr.cftime_range(start='2020-01-01', end='2021-12-31', freq='D')
xr.date_range(start=times[0], end=times[-1], freq='D', calendar=times.calendar)

Resulting in:

TypeError: Cannot convert input [2020-01-01 00:00:00] of type <class 'cftime._cftime.DatetimeGregorian'> to Timestamp

Because of the "standard" calendar and an error that doesn't match the one Xarray is looking for it uses the pd.date_range rather than the cftime equivalent and then fails.

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 by reproducing the example through xr.date_range with the cftime_range values and the standard calendar. Trace how xr.date_range chooses between pandas and cftime handling, then verify that this input no longer raises the shown TypeError and that the existing behavior for the equivalent paths remains intact.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.