Skip to content

Commit 56b2f0e

Browse files
jbrockmendeljreback
authored andcommitted
use npy_datetimestruct (#21886)
1 parent dcd9e6c commit 56b2f0e

18 files changed

+164
-170
lines changed

pandas/_libs/src/datetime/np_datetime.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
2828
#define PyInt_AsLong PyLong_AsLong
2929
#endif
3030

31-
const pandas_datetimestruct _NS_MIN_DTS = {
31+
const npy_datetimestruct _NS_MIN_DTS = {
3232
1677, 9, 21, 0, 12, 43, 145225, 0, 0};
33-
const pandas_datetimestruct _NS_MAX_DTS = {
33+
const npy_datetimestruct _NS_MAX_DTS = {
3434
2262, 4, 11, 23, 47, 16, 854775, 807000, 0};
3535

3636

@@ -62,7 +62,7 @@ int dayofweek(int y, int m, int d) {
6262
* Adjusts a datetimestruct based on a minutes offset. Assumes
6363
* the current values are valid.g
6464
*/
65-
void add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes) {
65+
void add_minutes_to_datetimestruct(npy_datetimestruct *dts, int minutes) {
6666
int isleap;
6767

6868
/* MINUTES */
@@ -111,7 +111,7 @@ void add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes) {
111111
/*
112112
* Calculates the days offset from the 1970 epoch.
113113
*/
114-
npy_int64 get_datetimestruct_days(const pandas_datetimestruct *dts) {
114+
npy_int64 get_datetimestruct_days(const npy_datetimestruct *dts) {
115115
int i, month;
116116
npy_int64 year, days = 0;
117117
const int *month_lengths;
@@ -211,7 +211,7 @@ static npy_int64 days_to_yearsdays(npy_int64 *days_) {
211211
* Adjusts a datetimestruct based on a seconds offset. Assumes
212212
* the current values are valid.
213213
*/
214-
NPY_NO_EXPORT void add_seconds_to_datetimestruct(pandas_datetimestruct *dts,
214+
NPY_NO_EXPORT void add_seconds_to_datetimestruct(npy_datetimestruct *dts,
215215
int seconds) {
216216
int minutes;
217217

@@ -236,7 +236,7 @@ NPY_NO_EXPORT void add_seconds_to_datetimestruct(pandas_datetimestruct *dts,
236236
* offset from 1970.
237237
*/
238238
static void set_datetimestruct_days(npy_int64 days,
239-
pandas_datetimestruct *dts) {
239+
npy_datetimestruct *dts) {
240240
const int *month_lengths;
241241
int i;
242242

@@ -255,10 +255,10 @@ static void set_datetimestruct_days(npy_int64 days,
255255
}
256256

257257
/*
258-
* Compares two pandas_datetimestruct objects chronologically
258+
* Compares two npy_datetimestruct objects chronologically
259259
*/
260-
int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
261-
const pandas_datetimestruct *b) {
260+
int cmp_npy_datetimestruct(const npy_datetimestruct *a,
261+
const npy_datetimestruct *b) {
262262
if (a->year > b->year) {
263263
return 1;
264264
} else if (a->year < b->year) {
@@ -319,7 +319,7 @@ int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
319319
/*
320320
*
321321
* Tests for and converts a Python datetime.datetime or datetime.date
322-
* object into a NumPy pandas_datetimestruct. Uses tzinfo (if present)
322+
* object into a NumPy npy_datetimestruct. Uses tzinfo (if present)
323323
* to convert to UTC time.
324324
*
325325
* While the C API has PyDate_* and PyDateTime_* functions, the following
@@ -331,12 +331,12 @@ int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
331331
* if obj doesn't have the needed date or datetime attributes.
332332
*/
333333
int convert_pydatetime_to_datetimestruct(PyObject *obj,
334-
pandas_datetimestruct *out) {
334+
npy_datetimestruct *out) {
335335
PyObject *tmp;
336336
int isleap;
337337

338338
/* Initialize the output to all zeros */
339-
memset(out, 0, sizeof(pandas_datetimestruct));
339+
memset(out, 0, sizeof(npy_datetimestruct));
340340
out->month = 1;
341341
out->day = 1;
342342

@@ -512,16 +512,16 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
512512
return -1;
513513
}
514514

515-
npy_datetime pandas_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
516-
pandas_datetimestruct *d) {
515+
npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
516+
npy_datetimestruct *d) {
517517
npy_datetime result = NPY_DATETIME_NAT;
518518

519519
convert_datetimestruct_to_datetime(fr, d, &result);
520520
return result;
521521
}
522522

523523
void pandas_datetime_to_datetimestruct(npy_datetime val, NPY_DATETIMEUNIT fr,
524-
pandas_datetimestruct *result) {
524+
npy_datetimestruct *result) {
525525
convert_datetime_to_datetimestruct(fr, val, result);
526526
}
527527

@@ -539,7 +539,7 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
539539
* Returns 0 on success, -1 on failure.
540540
*/
541541
int convert_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
542-
const pandas_datetimestruct *dts,
542+
const npy_datetimestruct *dts,
543543
npy_datetime *out) {
544544
npy_datetime ret;
545545

@@ -643,11 +643,11 @@ int convert_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
643643
*/
644644
int convert_datetime_to_datetimestruct(NPY_DATETIMEUNIT base,
645645
npy_datetime dt,
646-
pandas_datetimestruct *out) {
646+
npy_datetimestruct *out) {
647647
npy_int64 perday;
648648

649649
/* Initialize the output to all zeros */
650-
memset(out, 0, sizeof(pandas_datetimestruct));
650+
memset(out, 0, sizeof(npy_datetimestruct));
651651
out->year = 1970;
652652
out->month = 1;
653653
out->day = 1;

pandas/_libs/src/datetime/np_datetime.h

+12-17
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,25 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
1919

2020
#include <numpy/ndarraytypes.h>
2121

22-
typedef struct {
23-
npy_int64 year;
24-
npy_int32 month, day, hour, min, sec, us, ps, as;
25-
} pandas_datetimestruct;
26-
2722
typedef struct {
2823
npy_int64 days;
2924
npy_int32 hrs, min, sec, ms, us, ns, seconds, microseconds, nanoseconds;
3025
} pandas_timedeltastruct;
3126

32-
extern const pandas_datetimestruct _NS_MIN_DTS;
33-
extern const pandas_datetimestruct _NS_MAX_DTS;
27+
extern const npy_datetimestruct _NS_MIN_DTS;
28+
extern const npy_datetimestruct _NS_MAX_DTS;
3429

3530
// stuff pandas needs
3631
// ----------------------------------------------------------------------------
3732

3833
int convert_pydatetime_to_datetimestruct(PyObject *obj,
39-
pandas_datetimestruct *out);
34+
npy_datetimestruct *out);
4035

41-
npy_datetime pandas_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
42-
pandas_datetimestruct *d);
36+
npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
37+
npy_datetimestruct *d);
4338

4439
void pandas_datetime_to_datetimestruct(npy_datetime val, NPY_DATETIMEUNIT fr,
45-
pandas_datetimestruct *result);
40+
npy_datetimestruct *result);
4641

4742
void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
4843
NPY_DATETIMEUNIT fr,
@@ -61,27 +56,27 @@ int is_leapyear(npy_int64 year);
6156
* Calculates the days offset from the 1970 epoch.
6257
*/
6358
npy_int64
64-
get_datetimestruct_days(const pandas_datetimestruct *dts);
59+
get_datetimestruct_days(const npy_datetimestruct *dts);
6560

6661

6762
/*
68-
* Compares two pandas_datetimestruct objects chronologically
63+
* Compares two npy_datetimestruct objects chronologically
6964
*/
70-
int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
71-
const pandas_datetimestruct *b);
65+
int cmp_npy_datetimestruct(const npy_datetimestruct *a,
66+
const npy_datetimestruct *b);
7267

7368

7469
/*
7570
* Adjusts a datetimestruct based on a minutes offset. Assumes
7671
* the current values are valid.
7772
*/
7873
void
79-
add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes);
74+
add_minutes_to_datetimestruct(npy_datetimestruct *dts, int minutes);
8075

8176

8277
int
8378
convert_datetime_to_datetimestruct(NPY_DATETIMEUNIT base,
8479
npy_datetime dt,
85-
pandas_datetimestruct *out);
80+
npy_datetimestruct *out);
8681

8782
#endif // PANDAS__LIBS_SRC_DATETIME_NP_DATETIME_H_

pandas/_libs/src/datetime/np_datetime_strings.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ This file implements string parsing and creation for NumPy datetime.
6363
* Returns 0 on success, -1 on failure.
6464
*/
6565
int parse_iso_8601_datetime(char *str, int len,
66-
pandas_datetimestruct *out,
66+
npy_datetimestruct *out,
6767
int *out_local, int *out_tzoffset) {
6868
int year_leap = 0;
6969
int i, numdigits;
@@ -86,7 +86,7 @@ int parse_iso_8601_datetime(char *str, int len,
8686
int hour_was_2_digits = 0;
8787

8888
/* Initialize the output to all zeros */
89-
memset(out, 0, sizeof(pandas_datetimestruct));
89+
memset(out, 0, sizeof(npy_datetimestruct));
9090
out->month = 1;
9191
out->day = 1;
9292

@@ -567,7 +567,7 @@ int get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base) {
567567

568568

569569
/*
570-
* Converts an pandas_datetimestruct to an (almost) ISO 8601
570+
* Converts an npy_datetimestruct to an (almost) ISO 8601
571571
* NULL-terminated string using timezone Z (UTC). If the string fits in
572572
* the space exactly, it leaves out the NULL terminator and returns success.
573573
*
@@ -580,7 +580,7 @@ int get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base) {
580580
* Returns 0 on success, -1 on failure (for example if the output
581581
* string was too short).
582582
*/
583-
int make_iso_8601_datetime(pandas_datetimestruct *dts, char *outstr, int outlen,
583+
int make_iso_8601_datetime(npy_datetimestruct *dts, char *outstr, int outlen,
584584
NPY_DATETIMEUNIT base) {
585585
char *substr = outstr, sublen = outlen;
586586
int tmplen;

pandas/_libs/src/datetime/np_datetime_strings.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ This file implements string parsing and creation for NumPy datetime.
5151
*/
5252
int
5353
parse_iso_8601_datetime(char *str, int len,
54-
pandas_datetimestruct *out,
55-
int *out_local,
56-
int *out_tzoffset);
54+
npy_datetimestruct *out,
55+
int *out_local,
56+
int *out_tzoffset);
5757

5858
/*
5959
* Provides a string length to use for converting datetime
@@ -63,7 +63,7 @@ int
6363
get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base);
6464

6565
/*
66-
* Converts an pandas_datetimestruct to an (almost) ISO 8601
66+
* Converts an npy_datetimestruct to an (almost) ISO 8601
6767
* NULL-terminated string using timezone Z (UTC).
6868
*
6969
* 'base' restricts the output to that unit. Set 'base' to
@@ -73,7 +73,7 @@ get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base);
7373
* string was too short).
7474
*/
7575
int
76-
make_iso_8601_datetime(pandas_datetimestruct *dts, char *outstr, int outlen,
76+
make_iso_8601_datetime(npy_datetimestruct *dts, char *outstr, int outlen,
7777
NPY_DATETIMEUNIT base);
7878

7979
#endif // PANDAS__LIBS_SRC_DATETIME_NP_DATETIME_STRINGS_H_

pandas/_libs/src/period_helper.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ static int monthToQuarter(int month) { return ((month - 1) / 3) + 1; }
4747
* Assumes GREGORIAN_CALENDAR */
4848
npy_int64 unix_date_from_ymd(int year, int month, int day) {
4949
/* Calculate the absolute date */
50-
pandas_datetimestruct dts;
50+
npy_datetimestruct dts;
5151
npy_int64 unix_date;
5252

53-
memset(&dts, 0, sizeof(pandas_datetimestruct));
53+
memset(&dts, 0, sizeof(npy_datetimestruct));
5454
dts.year = year;
5555
dts.month = month;
5656
dts.day = day;
57-
unix_date = pandas_datetimestruct_to_datetime(NPY_FR_D, &dts);
57+
unix_date = npy_datetimestruct_to_datetime(NPY_FR_D, &dts);
5858
return unix_date;
5959
}
6060

@@ -127,7 +127,7 @@ static npy_int64 DtoB_weekday(npy_int64 unix_date) {
127127
return floordiv(unix_date + 4, 7) * 5 + mod_compat(unix_date + 4, 7) - 4;
128128
}
129129

130-
static npy_int64 DtoB(pandas_datetimestruct *dts,
130+
static npy_int64 DtoB(npy_datetimestruct *dts,
131131
int roll_back, npy_int64 unix_date) {
132132
int day_of_week = dayofweek(dts->year, dts->month, dts->day);
133133

@@ -149,7 +149,7 @@ static npy_int64 DtoB(pandas_datetimestruct *dts,
149149
//************ FROM DAILY ***************
150150

151151
static npy_int64 asfreq_DTtoA(npy_int64 ordinal, asfreq_info *af_info) {
152-
pandas_datetimestruct dts;
152+
npy_datetimestruct dts;
153153
ordinal = downsample_daytime(ordinal, af_info);
154154
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts);
155155
if (dts.month > af_info->to_end) {
@@ -160,7 +160,7 @@ static npy_int64 asfreq_DTtoA(npy_int64 ordinal, asfreq_info *af_info) {
160160
}
161161

162162
static int DtoQ_yq(npy_int64 ordinal, asfreq_info *af_info, int *year) {
163-
pandas_datetimestruct dts;
163+
npy_datetimestruct dts;
164164
int quarter;
165165

166166
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts);
@@ -188,7 +188,7 @@ static npy_int64 asfreq_DTtoQ(npy_int64 ordinal, asfreq_info *af_info) {
188188
}
189189

190190
static npy_int64 asfreq_DTtoM(npy_int64 ordinal, asfreq_info *af_info) {
191-
pandas_datetimestruct dts;
191+
npy_datetimestruct dts;
192192

193193
ordinal = downsample_daytime(ordinal, af_info);
194194

@@ -203,7 +203,7 @@ static npy_int64 asfreq_DTtoW(npy_int64 ordinal, asfreq_info *af_info) {
203203

204204
static npy_int64 asfreq_DTtoB(npy_int64 ordinal, asfreq_info *af_info) {
205205
int roll_back;
206-
pandas_datetimestruct dts;
206+
npy_datetimestruct dts;
207207
npy_int64 unix_date = downsample_daytime(ordinal, af_info);
208208
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);
209209

@@ -262,7 +262,7 @@ static npy_int64 asfreq_WtoW(npy_int64 ordinal, asfreq_info *af_info) {
262262

263263
static npy_int64 asfreq_WtoB(npy_int64 ordinal, asfreq_info *af_info) {
264264
int roll_back;
265-
pandas_datetimestruct dts;
265+
npy_datetimestruct dts;
266266
npy_int64 unix_date = asfreq_WtoDT(ordinal, af_info);
267267

268268
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);
@@ -302,7 +302,7 @@ static npy_int64 asfreq_MtoW(npy_int64 ordinal, asfreq_info *af_info) {
302302

303303
static npy_int64 asfreq_MtoB(npy_int64 ordinal, asfreq_info *af_info) {
304304
int roll_back;
305-
pandas_datetimestruct dts;
305+
npy_datetimestruct dts;
306306
npy_int64 unix_date = asfreq_MtoDT(ordinal, af_info);
307307

308308
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);
@@ -357,7 +357,7 @@ static npy_int64 asfreq_QtoW(npy_int64 ordinal, asfreq_info *af_info) {
357357

358358
static npy_int64 asfreq_QtoB(npy_int64 ordinal, asfreq_info *af_info) {
359359
int roll_back;
360-
pandas_datetimestruct dts;
360+
npy_datetimestruct dts;
361361
npy_int64 unix_date = asfreq_QtoDT(ordinal, af_info);
362362

363363
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);
@@ -414,7 +414,7 @@ static npy_int64 asfreq_AtoW(npy_int64 ordinal, asfreq_info *af_info) {
414414

415415
static npy_int64 asfreq_AtoB(npy_int64 ordinal, asfreq_info *af_info) {
416416
int roll_back;
417-
pandas_datetimestruct dts;
417+
npy_datetimestruct dts;
418418
npy_int64 unix_date = asfreq_AtoDT(ordinal, af_info);
419419

420420
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);

0 commit comments

Comments
 (0)