Skip to content

Commit 02e72ec

Browse files
jbrockmendeljreback
authored andcommitted
remove unused args, metadata structs (pandas-dev#18567)
1 parent 3e4e4b3 commit 02e72ec

File tree

4 files changed

+18
-98
lines changed

4 files changed

+18
-98
lines changed

pandas/_libs/src/datetime/np_datetime.c

+14-59
Original file line numberDiff line numberDiff line change
@@ -318,26 +318,19 @@ int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
318318
/*
319319
*
320320
* Tests for and converts a Python datetime.datetime or datetime.date
321-
* object into a NumPy pandas_datetimestruct.
321+
* object into a NumPy pandas_datetimestruct. Uses tzinfo (if present)
322+
* to convert to UTC time.
322323
*
323324
* While the C API has PyDate_* and PyDateTime_* functions, the following
324325
* implementation just asks for attributes, and thus supports
325326
* datetime duck typing. The tzinfo time zone conversion would require
326327
* this style of access anyway.
327328
*
328-
* 'out_bestunit' gives a suggested unit based on whether the object
329-
* was a datetime.date or datetime.datetime object.
330-
*
331-
* If 'apply_tzinfo' is 1, this function uses the tzinfo to convert
332-
* to UTC time, otherwise it returns the struct with the local time.
333-
*
334329
* Returns -1 on error, 0 on success, and 1 (with no error set)
335330
* if obj doesn't have the neeeded date or datetime attributes.
336331
*/
337332
int convert_pydatetime_to_datetimestruct(PyObject *obj,
338-
pandas_datetimestruct *out,
339-
PANDAS_DATETIMEUNIT *out_bestunit,
340-
int apply_tzinfo) {
333+
pandas_datetimestruct *out) {
341334
PyObject *tmp;
342335
int isleap;
343336

@@ -404,10 +397,6 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
404397
!PyObject_HasAttrString(obj, "minute") ||
405398
!PyObject_HasAttrString(obj, "second") ||
406399
!PyObject_HasAttrString(obj, "microsecond")) {
407-
/* The best unit for date is 'D' */
408-
if (out_bestunit != NULL) {
409-
*out_bestunit = PANDAS_FR_D;
410-
}
411400
return 0;
412401
}
413402

@@ -465,7 +454,7 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
465454
}
466455

467456
/* Apply the time zone offset if it exists */
468-
if (apply_tzinfo && PyObject_HasAttrString(obj, "tzinfo")) {
457+
if (PyObject_HasAttrString(obj, "tzinfo")) {
469458
tmp = PyObject_GetAttrString(obj, "tzinfo");
470459
if (tmp == NULL) {
471460
return -1;
@@ -506,11 +495,6 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
506495
}
507496
}
508497

509-
/* The resolution of Python's datetime is 'us' */
510-
if (out_bestunit != NULL) {
511-
*out_bestunit = PANDAS_FR_us;
512-
}
513-
514498
return 0;
515499

516500
invalid_date:
@@ -529,51 +513,34 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
529513

530514
npy_datetime pandas_datetimestruct_to_datetime(PANDAS_DATETIMEUNIT fr,
531515
pandas_datetimestruct *d) {
532-
pandas_datetime_metadata meta;
533516
npy_datetime result = PANDAS_DATETIME_NAT;
534517

535-
meta.base = fr;
536-
meta.num = 1;
537-
538-
convert_datetimestruct_to_datetime(&meta, d, &result);
518+
convert_datetimestruct_to_datetime(fr, d, &result);
539519
return result;
540520
}
541521

542522
void pandas_datetime_to_datetimestruct(npy_datetime val, PANDAS_DATETIMEUNIT fr,
543523
pandas_datetimestruct *result) {
544-
pandas_datetime_metadata meta;
545-
546-
meta.base = fr;
547-
meta.num = 1;
548-
549-
convert_datetime_to_datetimestruct(&meta, val, result);
524+
convert_datetime_to_datetimestruct(fr, val, result);
550525
}
551526

552527
void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
553528
PANDAS_DATETIMEUNIT fr,
554529
pandas_timedeltastruct *result) {
555-
pandas_datetime_metadata meta;
556-
557-
meta.base = fr;
558-
meta.num = 1;
559-
560-
convert_timedelta_to_timedeltastruct(&meta, val, result);
530+
convert_timedelta_to_timedeltastruct(fr, val, result);
561531
}
562532

563533

564534
/*
565535
* Converts a datetime from a datetimestruct to a datetime based
566-
* on some metadata. The date is assumed to be valid.
567-
*
568-
* TODO: If meta->num is really big, there could be overflow
536+
* on a metadata unit. The date is assumed to be valid.
569537
*
570538
* Returns 0 on success, -1 on failure.
571539
*/
572-
int convert_datetimestruct_to_datetime(pandas_datetime_metadata *meta,
540+
int convert_datetimestruct_to_datetime(PANDAS_DATETIMEUNIT base,
573541
const pandas_datetimestruct *dts,
574542
npy_datetime *out) {
575543
npy_datetime ret;
576-
PANDAS_DATETIMEUNIT base = meta->base;
577544

578545
if (base == PANDAS_FR_Y) {
579546
/* Truncate to the year */
@@ -665,15 +632,6 @@ int convert_datetimestruct_to_datetime(pandas_datetime_metadata *meta,
665632
}
666633
}
667634

668-
/* Divide by the multiplier */
669-
if (meta->num > 1) {
670-
if (ret >= 0) {
671-
ret /= meta->num;
672-
} else {
673-
ret = (ret - meta->num + 1) / meta->num;
674-
}
675-
}
676-
677635
*out = ret;
678636

679637
return 0;
@@ -682,7 +640,7 @@ int convert_datetimestruct_to_datetime(pandas_datetime_metadata *meta,
682640
/*
683641
* Converts a datetime based on the given metadata into a datetimestruct
684642
*/
685-
int convert_datetime_to_datetimestruct(pandas_datetime_metadata *meta,
643+
int convert_datetime_to_datetimestruct(PANDAS_DATETIMEUNIT base,
686644
npy_datetime dt,
687645
pandas_datetimestruct *out) {
688646
npy_int64 perday;
@@ -693,14 +651,11 @@ int convert_datetime_to_datetimestruct(pandas_datetime_metadata *meta,
693651
out->month = 1;
694652
out->day = 1;
695653

696-
/* TODO: Change to a mechanism that avoids the potential overflow */
697-
dt *= meta->num;
698-
699654
/*
700655
* Note that care must be taken with the / and % operators
701656
* for negative values.
702657
*/
703-
switch (meta->base) {
658+
switch (base) {
704659
case PANDAS_FR_Y:
705660
out->year = 1970 + dt;
706661
break;
@@ -902,11 +857,11 @@ int convert_datetime_to_datetimestruct(pandas_datetime_metadata *meta,
902857

903858
/*
904859
* Converts a timedelta from a timedeltastruct to a timedelta based
905-
* on some metadata. The timedelta is assumed to be valid.
860+
* on a metadata unit. The timedelta is assumed to be valid.
906861
*
907862
* Returns 0 on success, -1 on failure.
908863
*/
909-
int convert_timedelta_to_timedeltastruct(pandas_timedelta_metadata *meta,
864+
int convert_timedelta_to_timedeltastruct(PANDAS_DATETIMEUNIT base,
910865
npy_timedelta td,
911866
pandas_timedeltastruct *out) {
912867
npy_int64 frac;
@@ -918,7 +873,7 @@ int convert_timedelta_to_timedeltastruct(pandas_timedelta_metadata *meta,
918873
/* Initialize the output to all zeros */
919874
memset(out, 0, sizeof(pandas_timedeltastruct));
920875

921-
switch (meta->base) {
876+
switch (base) {
922877
case PANDAS_FR_ns:
923878

924879
// put frac in seconds

pandas/_libs/src/datetime/np_datetime.h

+2-32
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ typedef enum {
4040

4141
#define PANDAS_DATETIME_NUMUNITS 13
4242

43-
#define PANDAS_DATETIME_MAX_ISO8601_STRLEN (21+3*5+1+3*6+6+1)
44-
4543
#define PANDAS_DATETIME_NAT NPY_MIN_INT64
4644

4745
typedef struct {
@@ -54,23 +52,14 @@ typedef struct {
5452
npy_int32 hrs, min, sec, ms, us, ns, seconds, microseconds, nanoseconds;
5553
} pandas_timedeltastruct;
5654

57-
typedef struct {
58-
PANDAS_DATETIMEUNIT base;
59-
int num;
60-
} pandas_datetime_metadata;
61-
62-
typedef pandas_datetime_metadata pandas_timedelta_metadata;
63-
6455
extern const pandas_datetimestruct _NS_MIN_DTS;
6556
extern const pandas_datetimestruct _NS_MAX_DTS;
6657

6758
// stuff pandas needs
6859
// ----------------------------------------------------------------------------
6960

7061
int convert_pydatetime_to_datetimestruct(PyObject *obj,
71-
pandas_datetimestruct *out,
72-
PANDAS_DATETIMEUNIT *out_bestunit,
73-
int apply_tzinfo);
62+
pandas_datetimestruct *out);
7463

7564
npy_datetime pandas_datetimestruct_to_datetime(PANDAS_DATETIMEUNIT fr,
7665
pandas_datetimestruct *d);
@@ -91,19 +80,6 @@ extern const int days_per_month_table[2][12];
9180

9281
int is_leapyear(npy_int64 year);
9382

94-
/*
95-
* Converts a datetime from a datetimestruct to a datetime based
96-
* on some metadata. The date is assumed to be valid.
97-
*
98-
* TODO: If meta->num is really big, there could be overflow
99-
*
100-
* Returns 0 on success, -1 on failure.
101-
*/
102-
int
103-
convert_datetimestruct_to_datetime(pandas_datetime_metadata *meta,
104-
const pandas_datetimestruct *dts,
105-
npy_datetime *out);
106-
10783
/*
10884
* Calculates the days offset from the 1970 epoch.
10985
*/
@@ -127,14 +103,8 @@ add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes);
127103

128104

129105
int
130-
convert_datetime_to_datetimestruct(pandas_datetime_metadata *meta,
106+
convert_datetime_to_datetimestruct(PANDAS_DATETIMEUNIT base,
131107
npy_datetime dt,
132108
pandas_datetimestruct *out);
133109

134-
int
135-
convert_timedelta_to_timedeltastruct(pandas_timedelta_metadata *meta,
136-
npy_timedelta td,
137-
pandas_timedeltastruct *out);
138-
139-
140110
#endif // PANDAS__LIBS_SRC_DATETIME_NP_DATETIME_H_

pandas/_libs/src/datetime/np_datetime_strings.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,9 @@ int parse_iso_8601_datetime(char *str, int len, PANDAS_DATETIMEUNIT unit,
279279
if (len == 3 && tolower(str[0]) == 'n' && tolower(str[1]) == 'o' &&
280280
tolower(str[2]) == 'w') {
281281
NPY_TIME_T rawtime = 0;
282-
pandas_datetime_metadata meta;
283282

284283
time(&rawtime);
285284

286-
/* Set up a dummy metadata for the conversion */
287-
meta.base = PANDAS_FR_s;
288-
meta.num = 1;
289-
290285
bestunit = PANDAS_FR_s;
291286

292287
/*
@@ -304,7 +299,7 @@ int parse_iso_8601_datetime(char *str, int len, PANDAS_DATETIMEUNIT unit,
304299
*out_special = 1;
305300
}
306301

307-
return convert_datetime_to_datetimestruct(&meta, rawtime, out);
302+
return convert_datetime_to_datetimestruct(PANDAS_FR_s, rawtime, out);
308303
}
309304

310305
/* Anything else isn't a special value */

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ static void *PyDateTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
493493

494494
PRINTMARK();
495495

496-
if (!convert_pydatetime_to_datetimestruct(obj, &dts, NULL, 1)) {
496+
if (!convert_pydatetime_to_datetimestruct(obj, &dts)) {
497497
PRINTMARK();
498498
return PandasDateTimeStructToJSON(&dts, tc, outValue, _outLen);
499499
} else {

0 commit comments

Comments
 (0)