Skip to content

Relax nanosecond datetime restriction in CF time decoding - #9618

Merged
kmuehlbauer merged 165 commits into
pydata:mainfrom
kmuehlbauer:any-time-resolution-2
Jan 15, 2025
Merged

Relax nanosecond datetime restriction in CF time decoding#9618
kmuehlbauer merged 165 commits into
pydata:mainfrom
kmuehlbauer:any-time-resolution-2

Conversation

@kmuehlbauer

@kmuehlbauerkmuehlbauer commented Oct 13, 2024

Copy link
Copy Markdown
Contributor

This is another attempt to resolve#7493. This goes a step further than #9580.

The idea of this PR is to automatically infer the needed resolutions for decoding/encoding and only keep the constraints pandas imposes ("s" - lowest resolution, "ns" - highest resolution). There is still the idea of a default resolution, but this should only take precedence if it doesn't clash with the automatic inference. This can be discussed, though. Update: I've implemented time-unit-kwarg a first try to have default resolution on decode, which will override the current inferred resolution only to higher resolution (eg. 's' -> 'ns'). To work towards #4490 the time decoding options (decode_time and use_cftime are bundled within CFDatetimeCoder which is distributed via decode_times kwarg. use_cftime-kwarg is deprecated.

For sanity checking, and also for my own good, I've created a documentation page on time-coding in the internal dev section. Any suggestions (especially grammar) or ideas for enhancements are much appreciated.

There still might be room for consolidation of functions/methods (mostly in coding/times.py), but I have to leave it alone for some days. I went down that rabbit hole and need to relax, too 😬.

Looking forward to get your insights here, @spencerkclark, @ChrisBarker-NOAA, @pydata/xarray.

Todo:

  • floating point handling
  • update decoding tests to iterate over time_units (where appropriate)
  • CFDatetimeCoder as input for decode_times kwarg
  • ...

@kmuehlbauer

Copy link
Copy Markdown
ContributorAuthor

Nice, mypy 1.12 is out and breaks our typing, 😭.

@TomNicholas

Copy link
Copy Markdown
Member

Nice, mypy 1.12 is out and breaks our typing, 😭

Can we pin it in the CI temporarily?

@TomNicholasTomNicholas mentioned this pull request Oct 14, 2024
4 tasks
@kmuehlbauer

Copy link
Copy Markdown
ContributorAuthor

Can we pin it in the CI temporarily?

Yes, 1.11.2 was the last version.

@kmuehlbauer
kmuehlbauer marked this pull request as ready for review October 14, 2024 18:05
@kmuehlbauer

Copy link
Copy Markdown
ContributorAuthor

This is now ready for a first round of review. I think this is already in a quite usable state.

But no rush, this should be thoroughly tested.

@spencerkclark

Copy link
Copy Markdown
Member

Sounds good @kmuehlbauer! I’ll try and take an initial look this weekend.

spencerkclarkand others added 2 commits January 13, 2025 08:20
* Fix timedelta encoding overflow issue; always decode to ns resolution
* Implement time_unit for decode_cf_timedelta
* Reduce diff
@kmuehlbauer

Copy link
Copy Markdown
ContributorAuthor

Thanks again @spencerkclark! I've merged your kmuehlbauer#3 as it is essential for this PR to keep consistency and backwards compatibility.

Comment threaddoc/whats-new.rst Outdated
Comment threadxarray/tests/test_coding_times.py Outdated
Comment threadxarray/tests/test_coding_times.py Outdated
Comment threaddoc/whats-new.rst Outdated
Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>
@kmuehlbauer

Copy link
Copy Markdown
ContributorAuthor

Failing CI looks unrelated. Decision in the dev meeting was to merge fast to have a bit of time to test before release later this month.

@kmuehlbauer
kmuehlbauer merged commit 6bea715 into pydata:mainJan 15, 2025
@kmuehlbauer

Copy link
Copy Markdown
ContributorAuthor

Thanks All! 🚀

@spencerkclark

Copy link
Copy Markdown
Member

Awesome, congrats @kmuehlbauer—thank you!

@ilan-gold

Copy link
Copy Markdown
Contributor

Yes @kmuehlbauer - monumental effort!!!

@dcherian

Copy link
Copy Markdown
Contributor

Amazing! 👏 👏 👏

dcherian added a commit that referenced this pull request Jan 30, 2025
* main: (79 commits)
fix mean for datetime-like using the respective time resolution unit (#9977)
Add `time_unit` argument to `CFTimeIndex.to_datetimeindex` (#9965)
remove gate and add a test (#9958)
Remove repetitive that (replace it with the) (#9994)
add shxarray to the xarray ecosystem list (#9995)
Add `shards` to `valid_encodings` to enable sharded Zarr writing (#9948)
Use flox for grouped first, last (#9986)
Bump the actions group with 2 updates (#9989)
Fix some typing (#9988)
Remove unnecessary a article (#9980)
Fix test_doc_example on big-endian systems (#9949)
fix weighted polyfit for arrays with more than 2 dimensions (#9974)
Use zarr-fixture to prevent thread leakage errors (#9967)
remove dask-expr from CI runs, fix related tests (#9971)
Update time coding tests to assert exact equality (#9961)
cast type to PDDatetimeUnitOptions (#9963)
Suggest the correct name when no key matches in the dataset (#9943)
fix upstream dev issues (#9953)
Relax nanosecond datetime restriction in CF time decoding (#9618)
Remove outdated quantile test. (#9945)
...
dcherian added a commit that referenced this pull request Jan 30, 2025
* main: (79 commits)
fix mean for datetime-like using the respective time resolution unit (#9977)
Add `time_unit` argument to `CFTimeIndex.to_datetimeindex` (#9965)
remove gate and add a test (#9958)
Remove repetitive that (replace it with the) (#9994)
add shxarray to the xarray ecosystem list (#9995)
Add `shards` to `valid_encodings` to enable sharded Zarr writing (#9948)
Use flox for grouped first, last (#9986)
Bump the actions group with 2 updates (#9989)
Fix some typing (#9988)
Remove unnecessary a article (#9980)
Fix test_doc_example on big-endian systems (#9949)
fix weighted polyfit for arrays with more than 2 dimensions (#9974)
Use zarr-fixture to prevent thread leakage errors (#9967)
remove dask-expr from CI runs, fix related tests (#9971)
Update time coding tests to assert exact equality (#9961)
cast type to PDDatetimeUnitOptions (#9963)
Suggest the correct name when no key matches in the dataset (#9943)
fix upstream dev issues (#9953)
Relax nanosecond datetime restriction in CF time decoding (#9618)
Remove outdated quantile test. (#9945)
...
spencerkclark added a commit that referenced this pull request May 26, 2025
In #9618 we allowed encoding `np.datetime64` values prior to 1582-10-15 using a `"standard"` or `"gregorian"` calendar through cftime. While technically possible, this implicitly introduces a calendar change, and means the values can no longer be round tripped as `np.datetime64`—xarray will choose `cftime.DatetimeGregorian` instances when decoding instead. I am not sure how often this will come up and the behavior may not be the user's intent.
This PR switches to raising a `ValueError` in this circumstance, and recommends encoding with a `"proleptic_gregorian"` calendar instead (the calendar that xarray automatically chooses for `np.datetime64` values if provided no user input).
wavebyrd pushed a commit to wavebyrd/xarray that referenced this pull request Mar 13, 2026
* main: (79 commits)
fix mean for datetime-like using the respective time resolution unit (pydata#9977)
Add `time_unit` argument to `CFTimeIndex.to_datetimeindex` (pydata#9965)
remove gate and add a test (pydata#9958)
Remove repetitive that (replace it with the) (pydata#9994)
add shxarray to the xarray ecosystem list (pydata#9995)
Add `shards` to `valid_encodings` to enable sharded Zarr writing (pydata#9948)
Use flox for grouped first, last (pydata#9986)
Bump the actions group with 2 updates (pydata#9989)
Fix some typing (pydata#9988)
Remove unnecessary a article (pydata#9980)
Fix test_doc_example on big-endian systems (pydata#9949)
fix weighted polyfit for arrays with more than 2 dimensions (pydata#9974)
Use zarr-fixture to prevent thread leakage errors (pydata#9967)
remove dask-expr from CI runs, fix related tests (pydata#9971)
Update time coding tests to assert exact equality (pydata#9961)
cast type to PDDatetimeUnitOptions (pydata#9963)
Suggest the correct name when no key matches in the dataset (pydata#9943)
fix upstream dev issues (pydata#9953)
Relax nanosecond datetime restriction in CF time decoding (pydata#9618)
Remove outdated quantile test. (pydata#9945)
...
@kmuehlbauerkmuehlbauer mentioned this pull request Jun 3, 2026
4 tasks
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to mergeFinal call for commentsrun-upstreamRun upstream CItopic-CF conventionstopic-cftime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interoperability with Pandas 2.0 non-nanosecond datetime DataArray constructor still coerces to np.datetime64[ns], not cftime in 0.11.0

11 participants

@kmuehlbauer@TomNicholas@spencerkclark@ChrisBarker-NOAA@dcherian@qq492947833@rabernat@alippai@shoyer@ilan-gold@Illviljan