COMPAT: Adapt to Numpy 2.0 dtype changes#57774
COMPAT: Adapt to Numpy 2.0 dtype changes#57774lithomas1 wants to merge 5 commits intopandas-dev:mainfrom
Conversation
|
cc @seberg for any objections to the approach here. (I hope we aren't abusing numpy internals here). |
|
I think the migration guide suggests using - return (((PyArray_DatetimeDTypeMetaData *)dtype->c_metadata)->meta);
+ return PyDataType_C_METADATA(dtype);But what you have should also work. |
|
This is OK. I think slightly nicer would be to define I thought there would be an |
so the complete diff would be like this? - return (((PyArray_DatetimeDTypeMetaData *)dtype->c_metadata)->meta);
+ #if NPY_ABI_VERSION < 0x02000000
+ #define PyDataType_C_METADATA(descr) ((descr)->c_metadata)
+ #endif
+ return (PyDataType_C_METADATA(descr)->meta); |
Co-authored-by: Sebastian Berg <[email protected]>
Co-authored-by: Sebastian Berg <[email protected]>
|
Huh, Does it need to be exposed on the numpy side, or am I just missing something obvious? |
|
Maybe its something to do with the static inline declaration? |
|
Hummmm, weird. Any chance there is some caching involved in CI and it is using an older NumPy wheel? |
|
I had the same issue locally earlier today, and that was not because of caching I think (I tried with removing my pandas build directory multiple times) |
|
No, I have it locally too Here's what the header looks like (ndarraytypes.h) |
|
The whitespace is where the definition of the static things should be, but they're just gone. We still have macros like |
|
And I was certainly using an up to date numpy, because wasn't using a nightly wheel but locally built from latest main. For the changes in pyarrow, I did not run into that error. I was trying to see the difference between both, but couldn't directly see anything. |
|
Sorry I hadn't noticed: the definitions are moved, and you need I'll have a look at it. |
|
Sorry, I'll need a bit more time to set up the env for NumPy 2, but this diff should be right, I think. The annoying part is that we need to use Detailsdiff --git a/pandas/_libs/src/datetime/pd_datetime.c b/pandas/_libs/src/datetime/pd_datetime.c
index 19de51be6e..f778d06607 100644
--- a/pandas/_libs/src/datetime/pd_datetime.c
+++ b/pandas/_libs/src/datetime/pd_datetime.c
@@ -20,6 +20,8 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
#include <Python.h>
#include "datetime.h"
+/* Need to import_array for np_datetime.c (for NumPy 1.x support) */
+#include "numpy/ndarrayobject.h"
#include "pandas/datetime/pd_datetime.h"
#include "pandas/portable.h"
@@ -255,5 +257,6 @@ static struct PyModuleDef pandas_datetimemodule = {
PyMODINIT_FUNC PyInit_pandas_datetime(void) {
PyDateTime_IMPORT;
+ import_array();
return PyModuleDef_Init(&pandas_datetimemodule);
}
diff --git a/pandas/_libs/src/vendored/numpy/datetime/np_datetime.c b/pandas/_libs/src/vendored/numpy/datetime/np_datetime.c
index 277d01807f..d2853b4936 100644
--- a/pandas/_libs/src/vendored/numpy/datetime/np_datetime.c
+++ b/pandas/_libs/src/vendored/numpy/datetime/np_datetime.c
@@ -16,7 +16,7 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
// Licence at LICENSES/NUMPY_LICENSE
-#define NO_IMPORT
+#define NO_IMPORT_ARRAY
#ifndef NPY_NO_DEPRECATED_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
@@ -25,7 +25,7 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
#include <Python.h>
#include "pandas/vendored/numpy/datetime/np_datetime.h"
-#include <numpy/ndarraytypes.h>
+#include <numpy/ndarrayobject.h>
#include <numpy/npy_common.h>
#if defined(_WIN32) |
Thanks, I'll test this out. Thinking out loud, maybe a simpler way could just be to use the newly exposed Would there be any ABI problems with using numpy C API functions from a new version? |
|
I opened gh-57780 to iterate myself quickly, probably done, but lets see what crops up next... Since they are proper API Functions, if you use them you cannot support NumPy 1.x unfortunately. |
|
closing this then. Thanks for helping debug this! |
doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.