Skip to content

Commit f43e7a5

Browse files
authored
Remove NpyDateTimeToEpoch (#56276)
1 parent 5dab6ad commit f43e7a5

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

pandas/_libs/include/pandas/datetime/date_conversions.h

-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,4 @@ int scaleNanosecToUnit(npy_int64 *value, NPY_DATETIMEUNIT unit);
2121
char *int64ToIso(int64_t value, NPY_DATETIMEUNIT valueUnit,
2222
NPY_DATETIMEUNIT base, size_t *len);
2323

24-
// TODO(username): this function doesn't do a lot; should augment or
25-
// replace with scaleNanosecToUnit
26-
npy_datetime NpyDateTimeToEpoch(npy_datetime dt, NPY_DATETIMEUNIT base);
27-
2824
char *int64ToIsoDuration(int64_t value, size_t *len);

pandas/_libs/include/pandas/datetime/pd_datetime.h

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ typedef struct {
3535
const npy_datetimestruct *);
3636
int (*scaleNanosecToUnit)(npy_int64 *, NPY_DATETIMEUNIT);
3737
char *(*int64ToIso)(int64_t, NPY_DATETIMEUNIT, NPY_DATETIMEUNIT, size_t *);
38-
npy_datetime (*NpyDateTimeToEpoch)(npy_datetime, NPY_DATETIMEUNIT);
3938
char *(*PyDateTimeToIso)(PyObject *, NPY_DATETIMEUNIT, size_t *);
4039
npy_datetime (*PyDateTimeToEpoch)(PyObject *, NPY_DATETIMEUNIT);
4140
char *(*int64ToIsoDuration)(int64_t, size_t *);

pandas/_libs/src/datetime/date_conversions.c

-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ char *int64ToIso(int64_t value, NPY_DATETIMEUNIT valueUnit,
6969
return result;
7070
}
7171

72-
npy_datetime NpyDateTimeToEpoch(npy_datetime dt, NPY_DATETIMEUNIT base) {
73-
scaleNanosecToUnit(&dt, base);
74-
return dt;
75-
}
76-
7772
/* Converts the int64_t representation of a duration to ISO; mutates len */
7873
char *int64ToIsoDuration(int64_t value, size_t *len) {
7974
pandas_timedeltastruct tds;

pandas/_libs/src/datetime/pd_datetime.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,21 @@ static npy_datetime PyDateTimeToEpoch(PyObject *dt, NPY_DATETIMEUNIT base) {
171171
if (!PyErr_Occurred()) {
172172
PyErr_SetString(PyExc_ValueError,
173173
"Could not convert PyDateTime to numpy datetime");
174+
175+
return -1;
174176
}
175-
// TODO(username): is setting errMsg required?
176-
// ((JSONObjectEncoder *)tc->encoder)->errorMsg = "";
177-
// return NULL;
178177
}
179178

180179
npy_datetime npy_dt = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts);
181-
return NpyDateTimeToEpoch(npy_dt, base);
180+
if (scaleNanosecToUnit(&npy_dt, base) == -1) {
181+
PyErr_Format(PyExc_ValueError,
182+
"Call to scaleNanosecToUnit with value %" NPY_DATETIME_FMT
183+
" and base %d failed",
184+
npy_dt, base);
185+
186+
return -1;
187+
}
188+
return npy_dt;
182189
}
183190

184191
static int pandas_datetime_exec(PyObject *module) {
@@ -191,7 +198,6 @@ static int pandas_datetime_exec(PyObject *module) {
191198
capi->npy_datetimestruct_to_datetime = npy_datetimestruct_to_datetime;
192199
capi->scaleNanosecToUnit = scaleNanosecToUnit;
193200
capi->int64ToIso = int64ToIso;
194-
capi->NpyDateTimeToEpoch = NpyDateTimeToEpoch;
195201
capi->PyDateTimeToIso = PyDateTimeToIso;
196202
capi->PyDateTimeToEpoch = PyDateTimeToEpoch;
197203
capi->int64ToIsoDuration = int64ToIsoDuration;

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,12 @@ static char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
12861286
} else {
12871287
int size_of_cLabel = 21; // 21 chars for int 64
12881288
cLabel = PyObject_Malloc(size_of_cLabel);
1289-
snprintf(cLabel, size_of_cLabel, "%" NPY_DATETIME_FMT,
1290-
NpyDateTimeToEpoch(i8date, base));
1289+
if (scaleNanosecToUnit(&i8date, base) == -1) {
1290+
NpyArr_freeLabels(ret, num);
1291+
ret = 0;
1292+
break;
1293+
}
1294+
snprintf(cLabel, size_of_cLabel, "%" NPY_DATETIME_FMT, i8date);
12911295
len = strlen(cLabel);
12921296
}
12931297
}
@@ -1373,7 +1377,7 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
13731377
tc->prv = pc;
13741378

13751379
if (PyTypeNum_ISDATETIME(enc->npyType)) {
1376-
const int64_t longVal = *(npy_int64 *)enc->npyValue;
1380+
int64_t longVal = *(npy_int64 *)enc->npyValue;
13771381
if (longVal == get_nat()) {
13781382
tc->type = JT_NULL;
13791383
} else {
@@ -1389,7 +1393,10 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
13891393
tc->type = JT_UTF8;
13901394
} else {
13911395
NPY_DATETIMEUNIT base = ((PyObjectEncoder *)tc->encoder)->datetimeUnit;
1392-
pc->longValue = NpyDateTimeToEpoch(longVal, base);
1396+
if (scaleNanosecToUnit(&longVal, base) == -1) {
1397+
goto INVALID;
1398+
}
1399+
pc->longValue = longVal;
13931400
tc->type = JT_LONG;
13941401
}
13951402
}

0 commit comments

Comments
 (0)