Skip to content

Commit

Permalink
Remove NpyDateTimeToEpoch (#56276)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd authored Dec 1, 2023
1 parent 5dab6ad commit f43e7a5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
4 changes: 0 additions & 4 deletions pandas/_libs/include/pandas/datetime/date_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 0 additions & 1 deletion pandas/_libs/include/pandas/datetime/pd_datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
5 changes: 0 additions & 5 deletions pandas/_libs/src/datetime/date_conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions pandas/_libs/src/datetime/pd_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down

0 comments on commit f43e7a5

Please sign in to comment.