@@ -1280,13 +1280,10 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
1280
1280
NPY_DATETIMEUNIT dateUnit = NPY_FR_ns ;
1281
1281
if (PyTypeNum_ISDATETIME (type_num )) {
1282
1282
is_datetimelike = 1 ;
1283
- PyArray_VectorUnaryFunc * castfunc =
1284
- PyArray_GetCastFunc (PyArray_DescrFromType (type_num ), NPY_INT64 );
1285
- if (!castfunc ) {
1286
- PyErr_Format (PyExc_ValueError , "Cannot cast numpy dtype %d to long" ,
1287
- enc -> npyType );
1288
- }
1289
- castfunc (dataptr , & i8date , 1 , NULL , NULL );
1283
+ PyObject * scalarItem = PyArray_ToScalar (dataptr , labels );
1284
+ PyArray_CastScalarToCtype (scalarItem , & i8date ,
1285
+ PyArray_DescrFromType (NPY_INT64 ));
1286
+ Py_DECREF (scalarItem );
1290
1287
dateUnit = get_datetime_metadata_from_dtype (dtype ).base ;
1291
1288
} else if (PyDate_Check (item ) || PyDelta_Check (item )) {
1292
1289
is_datetimelike = 1 ;
@@ -1432,6 +1429,13 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1432
1429
enc -> npyType );
1433
1430
}
1434
1431
castfunc (enc -> npyValue , & longVal , 1 , NULL , NULL );
1432
+
1433
+ PyObject * scalar =
1434
+ PyArray_ToScalar (enc -> npyValue , enc -> npyCtxtPassthru -> array );
1435
+ PyArray_Scalar (enc -> npyValue , PyArray_DescrFromType (enc -> npyType ), NULL );
1436
+ PyArray_CastScalarToCtype (scalar , & longVal ,
1437
+ PyArray_DescrFromType (NPY_INT64 ));
1438
+ Py_DECREF (scalar );
1435
1439
if (longVal == get_nat ()) {
1436
1440
tc -> type = JT_NULL ;
1437
1441
} else {
@@ -1532,7 +1536,6 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1532
1536
1533
1537
PyArray_Descr * outcode = PyArray_DescrFromType (NPY_INT64 );
1534
1538
PyArray_CastScalarToCtype (obj , & longVal , outcode );
1535
- Py_DECREF (outcode );
1536
1539
1537
1540
if (enc -> datetimeIso ) {
1538
1541
GET_TC (tc )-> longValue = longVal ;
0 commit comments