Skip to content

Commit d11c110

Browse files
committed
Backport PR pandas-dev#56114: CLN: Get rid of PyArray_GetCastFunc
1 parent f99a1fc commit d11c110

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

pandas/_libs/src/vendored/ujson/python/objToJSON.c

+17-25
Original file line numberDiff line numberDiff line change
@@ -1306,14 +1306,7 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
13061306
NPY_DATETIMEUNIT dateUnit = NPY_FR_ns;
13071307
if (PyTypeNum_ISDATETIME(type_num)) {
13081308
is_datetimelike = 1;
1309-
PyArray_VectorUnaryFunc *castfunc =
1310-
PyArray_GetCastFunc(PyArray_DescrFromType(type_num), NPY_INT64);
1311-
if (!castfunc) {
1312-
PyErr_Format(PyExc_ValueError,
1313-
"Cannot cast numpy dtype %d to long",
1314-
enc->npyType);
1315-
}
1316-
castfunc(dataptr, &i8date, 1, NULL, NULL);
1309+
i8date = *(npy_int64 *)dataptr;
13171310
dateUnit = get_datetime_metadata_from_dtype(dtype).base;
13181311
} else if (PyDate_Check(item) || PyDelta_Check(item)) {
13191312
is_datetimelike = 1;
@@ -1444,24 +1437,23 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
14441437
return;
14451438
}
14461439

1447-
pc = createTypeContext();
1448-
if (!pc) {
1449-
tc->type = JT_INVALID;
1450-
return;
1451-
}
1452-
tc->prv = pc;
1440+
pc = createTypeContext();
1441+
if (!pc) {
1442+
tc->type = JT_INVALID;
1443+
return;
1444+
}
1445+
tc->prv = pc;
14531446

1454-
if (PyTypeNum_ISDATETIME(enc->npyType)) {
1455-
int64_t longVal;
1456-
PyArray_VectorUnaryFunc *castfunc =
1457-
PyArray_GetCastFunc(PyArray_DescrFromType(enc->npyType), NPY_INT64);
1458-
if (!castfunc) {
1459-
PyErr_Format(PyExc_ValueError, "Cannot cast numpy dtype %d to long",
1460-
enc->npyType);
1461-
}
1462-
castfunc(enc->npyValue, &longVal, 1, NULL, NULL);
1463-
if (longVal == get_nat()) {
1464-
tc->type = JT_NULL;
1447+
if (PyTypeNum_ISDATETIME(enc->npyType)) {
1448+
int64_t longVal;
1449+
1450+
longVal = *(npy_int64 *)enc->npyValue;
1451+
if (longVal == get_nat()) {
1452+
tc->type = JT_NULL;
1453+
} else {
1454+
if (enc->datetimeIso) {
1455+
if (enc->npyType == NPY_TIMEDELTA) {
1456+
pc->PyTypeToUTF8 = NpyTimeDeltaToIsoCallback;
14651457
} else {
14661458
if (enc->datetimeIso) {
14671459
if (enc->npyType == NPY_TIMEDELTA) {

0 commit comments

Comments
 (0)