Avoid generic-unit timedelta in CDFepoch.to_datetime (fixes #333)#335
Open
gaoflow wants to merge 1 commit into
Open
Avoid generic-unit timedelta in CDFepoch.to_datetime (fixes #333)#335gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
Composing the datetime started the component sum from the implicit
integer 0 and used a bare np.datetime64("NaT"); both produce a
generic-unit timedelta, which NumPy >= 2.5 deprecates and will later
turn into an error. Start the sum from the first array and give NaT
the same unit as the data.
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.
Summary
CDFepoch.to_datetimeraises aDeprecationWarningunder NumPy 2.5+:Reproducer from #333:
Cause
In
_compose_date(cdflib/epochs.py):total_datetime = np.array(sum(arrays))—sumstarts from the integer0, so the first operation is0 + datetime64[...]. Adding a bare integer to a datetime64 yields a generic-unit timedelta, which NumPy now deprecates.np.where(nat_positions, np.datetime64('NaT'), total_datetime)— the barenp.datetime64('NaT')is also generic-unit and triggers the same warning when combined with the typedtotal_datetime.Fix
datetime64) instead of the implicit0.NaTwith the same unit as the data (np.datetime_data(total_datetime.dtype)[0]).Both are behaviour-preserving — only the intermediate dtypes change, not the result.
Verification
2.6.0.dev); after the fix,to_datetimeemits nogeneric-unitDeprecationWarningand returns the identical value.test_to_datetime_no_generic_timedelta_warning, which fails on the current code under the nightly and passes with the fix (and passes trivially on older NumPy that doesn't emit the warning).