From f43e7a5b986c14a9c5457c765b7695e363e19484 Mon Sep 17 00:00:00 2001 From: William Ayd Date: Fri, 1 Dec 2023 10:34:37 -0800 Subject: [PATCH] Remove NpyDateTimeToEpoch (#56276) --- .../include/pandas/datetime/date_conversions.h | 4 ---- .../_libs/include/pandas/datetime/pd_datetime.h | 1 - pandas/_libs/src/datetime/date_conversions.c | 5 ----- pandas/_libs/src/datetime/pd_datetime.c | 16 +++++++++++----- .../_libs/src/vendored/ujson/python/objToJSON.c | 15 +++++++++++---- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/pandas/_libs/include/pandas/datetime/date_conversions.h b/pandas/_libs/include/pandas/datetime/date_conversions.h index 42a16f33cc2ea..9a4a02ea89b4d 100644 --- a/pandas/_libs/include/pandas/datetime/date_conversions.h +++ b/pandas/_libs/include/pandas/datetime/date_conversions.h @@ -21,8 +21,4 @@ int scaleNanosecToUnit(npy_int64 *value, NPY_DATETIMEUNIT unit); char *int64ToIso(int64_t value, NPY_DATETIMEUNIT valueUnit, NPY_DATETIMEUNIT base, size_t *len); -// TODO(username): this function doesn't do a lot; should augment or -// replace with scaleNanosecToUnit -npy_datetime NpyDateTimeToEpoch(npy_datetime dt, NPY_DATETIMEUNIT base); - char *int64ToIsoDuration(int64_t value, size_t *len); diff --git a/pandas/_libs/include/pandas/datetime/pd_datetime.h b/pandas/_libs/include/pandas/datetime/pd_datetime.h index 714d264924750..7674fbbe743fe 100644 --- a/pandas/_libs/include/pandas/datetime/pd_datetime.h +++ b/pandas/_libs/include/pandas/datetime/pd_datetime.h @@ -35,7 +35,6 @@ typedef struct { const npy_datetimestruct *); int (*scaleNanosecToUnit)(npy_int64 *, NPY_DATETIMEUNIT); char *(*int64ToIso)(int64_t, NPY_DATETIMEUNIT, NPY_DATETIMEUNIT, size_t *); - npy_datetime (*NpyDateTimeToEpoch)(npy_datetime, NPY_DATETIMEUNIT); char *(*PyDateTimeToIso)(PyObject *, NPY_DATETIMEUNIT, size_t *); npy_datetime (*PyDateTimeToEpoch)(PyObject *, NPY_DATETIMEUNIT); char *(*int64ToIsoDuration)(int64_t, size_t *); diff --git a/pandas/_libs/src/datetime/date_conversions.c b/pandas/_libs/src/datetime/date_conversions.c index 4b172349de8d3..7eaf8aad12f43 100644 --- a/pandas/_libs/src/datetime/date_conversions.c +++ b/pandas/_libs/src/datetime/date_conversions.c @@ -69,11 +69,6 @@ char *int64ToIso(int64_t value, NPY_DATETIMEUNIT valueUnit, return result; } -npy_datetime NpyDateTimeToEpoch(npy_datetime dt, NPY_DATETIMEUNIT base) { - scaleNanosecToUnit(&dt, base); - return dt; -} - /* Converts the int64_t representation of a duration to ISO; mutates len */ char *int64ToIsoDuration(int64_t value, size_t *len) { pandas_timedeltastruct tds; diff --git a/pandas/_libs/src/datetime/pd_datetime.c b/pandas/_libs/src/datetime/pd_datetime.c index b201023114f8a..030d734aeab21 100644 --- a/pandas/_libs/src/datetime/pd_datetime.c +++ b/pandas/_libs/src/datetime/pd_datetime.c @@ -171,14 +171,21 @@ static npy_datetime PyDateTimeToEpoch(PyObject *dt, NPY_DATETIMEUNIT base) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, "Could not convert PyDateTime to numpy datetime"); + + return -1; } - // TODO(username): is setting errMsg required? - // ((JSONObjectEncoder *)tc->encoder)->errorMsg = ""; - // return NULL; } npy_datetime npy_dt = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts); - return NpyDateTimeToEpoch(npy_dt, base); + if (scaleNanosecToUnit(&npy_dt, base) == -1) { + PyErr_Format(PyExc_ValueError, + "Call to scaleNanosecToUnit with value %" NPY_DATETIME_FMT + " and base %d failed", + npy_dt, base); + + return -1; + } + return npy_dt; } static int pandas_datetime_exec(PyObject *module) { @@ -191,7 +198,6 @@ static int pandas_datetime_exec(PyObject *module) { capi->npy_datetimestruct_to_datetime = npy_datetimestruct_to_datetime; capi->scaleNanosecToUnit = scaleNanosecToUnit; capi->int64ToIso = int64ToIso; - capi->NpyDateTimeToEpoch = NpyDateTimeToEpoch; capi->PyDateTimeToIso = PyDateTimeToIso; capi->PyDateTimeToEpoch = PyDateTimeToEpoch; capi->int64ToIsoDuration = int64ToIsoDuration; diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 6271791fe201e..9f1c1d3f857d1 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1286,8 +1286,12 @@ static char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc, } else { int size_of_cLabel = 21; // 21 chars for int 64 cLabel = PyObject_Malloc(size_of_cLabel); - snprintf(cLabel, size_of_cLabel, "%" NPY_DATETIME_FMT, - NpyDateTimeToEpoch(i8date, base)); + if (scaleNanosecToUnit(&i8date, base) == -1) { + NpyArr_freeLabels(ret, num); + ret = 0; + break; + } + snprintf(cLabel, size_of_cLabel, "%" NPY_DATETIME_FMT, i8date); len = strlen(cLabel); } } @@ -1373,7 +1377,7 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { tc->prv = pc; if (PyTypeNum_ISDATETIME(enc->npyType)) { - const int64_t longVal = *(npy_int64 *)enc->npyValue; + int64_t longVal = *(npy_int64 *)enc->npyValue; if (longVal == get_nat()) { tc->type = JT_NULL; } else { @@ -1389,7 +1393,10 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { tc->type = JT_UTF8; } else { NPY_DATETIMEUNIT base = ((PyObjectEncoder *)tc->encoder)->datetimeUnit; - pc->longValue = NpyDateTimeToEpoch(longVal, base); + if (scaleNanosecToUnit(&longVal, base) == -1) { + goto INVALID; + } + pc->longValue = longVal; tc->type = JT_LONG; } }