Skip to content

Avoid generic-unit timedelta in CDFepoch.to_datetime (fixes #333)#335

Open
gaoflow wants to merge 1 commit into
lasp:mainfrom
gaoflow:fix-333-generic-timedelta-deprecation
Open

Avoid generic-unit timedelta in CDFepoch.to_datetime (fixes #333)#335
gaoflow wants to merge 1 commit into
lasp:mainfrom
gaoflow:fix-333-generic-timedelta-deprecation

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 2, 2026

Copy link
Copy Markdown

Summary

CDFepoch.to_datetime raises a DeprecationWarning under NumPy 2.5+:

DeprecationWarning: The 'generic' unit for NumPy timedelta is deprecated, and will
raise an error in the future. This includes implicit conversion of bare integers
(e.g. `+ 1`). Please use a specific unit instead.

Reproducer from #333:

import warnings
warnings.simplefilter('always')
from cdflib.epochs import CDFepoch
CDFepoch.to_datetime(631377279184000000)   # warns twice

Cause

In _compose_date (cdflib/epochs.py):

  • total_datetime = np.array(sum(arrays))sum starts from the integer 0, so the first operation is 0 + 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 bare np.datetime64('NaT') is also generic-unit and triggers the same warning when combined with the typed total_datetime.

Fix

  • Sum the components starting from the first array (a datetime64) instead of the implicit 0.
  • Build NaT with 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

  • Reproduced the two warnings on a NumPy nightly (2.6.0.dev); after the fix, to_datetime emits no generic-unit DeprecationWarning and returns the identical value.
  • Full test suite green on released NumPy 2.4.6 (82 passed); the epoch tests pass on both released and nightly NumPy.
  • Added 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).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant