Skip to content

COMPAT: Adapt to Numpy 2.0 dtype changes #57780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/_libs/src/datetime/pd_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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 only) */
#define PY_ARRAY_UNIQUE_SYMBOL PANDAS_DATETIME_NUMPY
#include "numpy/ndarrayobject.h"
#include "pandas/datetime/pd_datetime.h"
#include "pandas/portable.h"

Expand Down Expand Up @@ -255,5 +258,6 @@ static struct PyModuleDef pandas_datetimemodule = {

PyMODINIT_FUNC PyInit_pandas_datetime(void) {
PyDateTime_IMPORT;
import_array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this needs to be called any time you import ndarrayobject.h right? If so, I wonder if it wouldn't be cleaner to move the include here to the header. That way other module that need these datetime functions can simply include / link the pd_datetime capsule and not have to include ndarrayobject.h directly. May be less confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes (if you use any API, but if you don't you only need ndarraytypes.h) but once per compilation unit only (just fixed that).
Places that use pd_datetime.h don't need it (we just need to be sure the init code ran).

return PyModuleDef_Init(&pandas_datetimemodule);
}
12 changes: 8 additions & 4 deletions pandas/_libs/src/vendored/numpy/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt

// Licence at LICENSES/NUMPY_LICENSE

#define NO_IMPORT

#ifndef NPY_NO_DEPRECATED_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#endif // NPY_NO_DEPRECATED_API

#include <Python.h>

#include "pandas/vendored/numpy/datetime/np_datetime.h"
#include <numpy/ndarraytypes.h>

#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL PANDAS_DATETIME_NUMPY
#include <numpy/ndarrayobject.h>
#include <numpy/npy_common.h>

#if defined(_WIN32)
Expand Down Expand Up @@ -1070,5 +1071,8 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta td,
*/
PyArray_DatetimeMetaData
get_datetime_metadata_from_dtype(PyArray_Descr *dtype) {
return (((PyArray_DatetimeDTypeMetaData *)dtype->c_metadata)->meta);
#if NPY_ABI_VERSION < 0x02000000
#define PyDataType_C_METADATA(dtype) ((dtype)->c_metadata)
#endif
return ((PyArray_DatetimeDTypeMetaData *)PyDataType_C_METADATA(dtype))->meta;
}