Usage of Datetime DType in NumPy 2.0 C-API

We have a C-extension module that works with Datetime DType objects through the C-API. The existing code works fine for NumPy 1x, but not 2x. The NumPy 2.0 migration documentation <https://numpy.org/doc/stable/numpy_2_0_migration_guide.html#the-pyarray-desc...> does not address Datetime DType. This line of code works for 1x: NPY_DATETIMEUNIT dtu = ((PyArray_DatetimeDTypeMetaData *)descr->c_metadata)->meta.base; Here, descr is a PyArray_Descr and the code accesses the datetime's unit value. The documentation notes that in 2x, this struct is now opaque. I updated this line of code to use the provided helper macro: NPY_DATETIMEUNIT dtu = ((PyArray_DatetimeDTypeMetaData *)((_PyArray_LegacyDescr *) PyDataType_C_METADATA(descr))->c_metadata)->meta.base; The code compiles, but raises the exception "Windows fatal exception: access violation". I changed the line of code to: NPY_DATETIMEUNIT dtu = ((PyArray_DatetimeDTypeMetaData *)((_PyArray_LegacyDescr *)descr)->c_metadata)->meta.base; This code compiles and runs, but _PyArray_LegacyDescr is semi-private: [image: image.png] And, the helper macro NPY_DT_is_legacy is not in the public API. What is the correct usage pattern for accessing Datetime DType descriptor fields? Lee

participants (2)
-
Lee Johnston
-
Sebastian Berg