Repository navigation
Fix time bounds KeyError for cftime coordinates without a calendar encoding - #859
Open
JOhnsonKC201 wants to merge 2 commits into
Open
JOhnsonKC201 wants to merge 2 commits into
JOhnsonKC201 wants to merge 2 commits into
Conversation
`_create_time_bounds()` read the calendar from `time.encoding["calendar"]`, which is only set when Xarray decodes times from a file that has a calendar attribute. Time coordinates created in memory, returned by operations such as `resample()` or `convert_calendar()`, or decoded from a file without a calendar attribute have no calendar encoding, so `add_time_bounds()` and `add_missing_bounds()` raised `KeyError: 'calendar'`. Get the calendar from the `cftime` objects with `.dt.calendar` instead.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adding time bounds to
cftimetime coordinates fails withKeyError: 'calendar'when the coordinates have no"calendar"key in theirencoding. That is the case for time coordinates built in memory, coordinates returned by Xarray operations likeresample()orconvert_calendar(), and files without acalendarattribute that plainxr.open_dataset()decodes tocftime. I didn't find an existing issue for this.Since "T" is in the default
axesofadd_missing_bounds()and onlyValueError/TypeErrorare skipped there, a plainds.bounds.add_missing_bounds()on such a dataset fails too.add_time_bounds()raises the same error.The cause is that
_create_time_bounds()readstime.encoding["calendar"]to pick thecftimedate type, but that key only exists when the times were decoded from a file with a calendar attribute. Thecftimeobjects always carry their own calendar, so I changed it to usetime.dt.calendar. That also guarantees the bounds have the same calendar as the coordinates they are compared against.I added a test that mirrors
test_returns_bounds_for_cftime_time_coords_using_freqwithout setting the encoding. It fails onmainwith theKeyErrorand passes with the fix. The rest of the suite andpre-commit run --all-filespass locally. I ran the tests in a pip environment without ESMF, so the 9 xESMF regridding tests could not run there; they fail the same way onmainin that environment.TemporalAccessorhandles the same situation by defaulting to the "standard" calendar with a warning, so for example a daily climatology on 360-day data without a calendar encoding raisesValueError: invalid day number. I left that alone here to keep this focused, but I'm happy to follow up if you'd like it to use.dt.calendaras well.Checklist
If applicable: