Skip to content

use npy_datetimestruct instead of pandas_datetimestruct #21886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions pandas/_libs/src/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
#define PyInt_AsLong PyLong_AsLong
#endif

const pandas_datetimestruct _NS_MIN_DTS = {
const npy_datetimestruct _NS_MIN_DTS = {
1677, 9, 21, 0, 12, 43, 145225, 0, 0};
const pandas_datetimestruct _NS_MAX_DTS = {
const npy_datetimestruct _NS_MAX_DTS = {
2262, 4, 11, 23, 47, 16, 854775, 807000, 0};


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

/* MINUTES */
Expand Down Expand Up @@ -111,7 +111,7 @@ void add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes) {
/*
* Calculates the days offset from the 1970 epoch.
*/
npy_int64 get_datetimestruct_days(const pandas_datetimestruct *dts) {
npy_int64 get_datetimestruct_days(const npy_datetimestruct *dts) {
int i, month;
npy_int64 year, days = 0;
const int *month_lengths;
Expand Down Expand Up @@ -211,7 +211,7 @@ static npy_int64 days_to_yearsdays(npy_int64 *days_) {
* Adjusts a datetimestruct based on a seconds offset. Assumes
* the current values are valid.
*/
NPY_NO_EXPORT void add_seconds_to_datetimestruct(pandas_datetimestruct *dts,
NPY_NO_EXPORT void add_seconds_to_datetimestruct(npy_datetimestruct *dts,
int seconds) {
int minutes;

Expand All @@ -236,7 +236,7 @@ NPY_NO_EXPORT void add_seconds_to_datetimestruct(pandas_datetimestruct *dts,
* offset from 1970.
*/
static void set_datetimestruct_days(npy_int64 days,
pandas_datetimestruct *dts) {
npy_datetimestruct *dts) {
const int *month_lengths;
int i;

Expand All @@ -255,10 +255,10 @@ static void set_datetimestruct_days(npy_int64 days,
}

/*
* Compares two pandas_datetimestruct objects chronologically
* Compares two npy_datetimestruct objects chronologically
*/
int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
const pandas_datetimestruct *b) {
int cmp_npy_datetimestruct(const npy_datetimestruct *a,
const npy_datetimestruct *b) {
if (a->year > b->year) {
return 1;
} else if (a->year < b->year) {
Expand Down Expand Up @@ -319,7 +319,7 @@ int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
/*
*
* Tests for and converts a Python datetime.datetime or datetime.date
* object into a NumPy pandas_datetimestruct. Uses tzinfo (if present)
* object into a NumPy npy_datetimestruct. Uses tzinfo (if present)
* to convert to UTC time.
*
* While the C API has PyDate_* and PyDateTime_* functions, the following
Expand All @@ -331,12 +331,12 @@ int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
* if obj doesn't have the needed date or datetime attributes.
*/
int convert_pydatetime_to_datetimestruct(PyObject *obj,
pandas_datetimestruct *out) {
npy_datetimestruct *out) {
PyObject *tmp;
int isleap;

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

Expand Down Expand Up @@ -512,16 +512,16 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
return -1;
}

npy_datetime pandas_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
pandas_datetimestruct *d) {
npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
npy_datetimestruct *d) {
npy_datetime result = NPY_DATETIME_NAT;

convert_datetimestruct_to_datetime(fr, d, &result);
return result;
}

void pandas_datetime_to_datetimestruct(npy_datetime val, NPY_DATETIMEUNIT fr,
pandas_datetimestruct *result) {
npy_datetimestruct *result) {
convert_datetime_to_datetimestruct(fr, val, result);
}

Expand All @@ -539,7 +539,7 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
* Returns 0 on success, -1 on failure.
*/
int convert_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
const pandas_datetimestruct *dts,
const npy_datetimestruct *dts,
npy_datetime *out) {
npy_datetime ret;

Expand Down Expand Up @@ -643,11 +643,11 @@ int convert_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
*/
int convert_datetime_to_datetimestruct(NPY_DATETIMEUNIT base,
npy_datetime dt,
pandas_datetimestruct *out) {
npy_datetimestruct *out) {
npy_int64 perday;

/* Initialize the output to all zeros */
memset(out, 0, sizeof(pandas_datetimestruct));
memset(out, 0, sizeof(npy_datetimestruct));
out->year = 1970;
out->month = 1;
out->day = 1;
Expand Down
29 changes: 12 additions & 17 deletions pandas/_libs/src/datetime/np_datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,25 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt

#include <numpy/ndarraytypes.h>

typedef struct {
npy_int64 year;
npy_int32 month, day, hour, min, sec, us, ps, as;
} pandas_datetimestruct;

typedef struct {
npy_int64 days;
npy_int32 hrs, min, sec, ms, us, ns, seconds, microseconds, nanoseconds;
} pandas_timedeltastruct;

extern const pandas_datetimestruct _NS_MIN_DTS;
extern const pandas_datetimestruct _NS_MAX_DTS;
extern const npy_datetimestruct _NS_MIN_DTS;
extern const npy_datetimestruct _NS_MAX_DTS;

// stuff pandas needs
// ----------------------------------------------------------------------------

int convert_pydatetime_to_datetimestruct(PyObject *obj,
pandas_datetimestruct *out);
npy_datetimestruct *out);

npy_datetime pandas_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
pandas_datetimestruct *d);
npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
npy_datetimestruct *d);

void pandas_datetime_to_datetimestruct(npy_datetime val, NPY_DATETIMEUNIT fr,
pandas_datetimestruct *result);
npy_datetimestruct *result);

void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
NPY_DATETIMEUNIT fr,
Expand All @@ -61,27 +56,27 @@ int is_leapyear(npy_int64 year);
* Calculates the days offset from the 1970 epoch.
*/
npy_int64
get_datetimestruct_days(const pandas_datetimestruct *dts);
get_datetimestruct_days(const npy_datetimestruct *dts);


/*
* Compares two pandas_datetimestruct objects chronologically
* Compares two npy_datetimestruct objects chronologically
*/
int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
const pandas_datetimestruct *b);
int cmp_npy_datetimestruct(const npy_datetimestruct *a,
const npy_datetimestruct *b);


/*
* Adjusts a datetimestruct based on a minutes offset. Assumes
* the current values are valid.
*/
void
add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes);
add_minutes_to_datetimestruct(npy_datetimestruct *dts, int minutes);


int
convert_datetime_to_datetimestruct(NPY_DATETIMEUNIT base,
npy_datetime dt,
pandas_datetimestruct *out);
npy_datetimestruct *out);

#endif // PANDAS__LIBS_SRC_DATETIME_NP_DATETIME_H_
8 changes: 4 additions & 4 deletions pandas/_libs/src/datetime/np_datetime_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ This file implements string parsing and creation for NumPy datetime.
* Returns 0 on success, -1 on failure.
*/
int parse_iso_8601_datetime(char *str, int len,
pandas_datetimestruct *out,
npy_datetimestruct *out,
int *out_local, int *out_tzoffset) {
int year_leap = 0;
int i, numdigits;
Expand All @@ -86,7 +86,7 @@ int parse_iso_8601_datetime(char *str, int len,
int hour_was_2_digits = 0;

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

Expand Down Expand Up @@ -567,7 +567,7 @@ int get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base) {


/*
* Converts an pandas_datetimestruct to an (almost) ISO 8601
* Converts an npy_datetimestruct to an (almost) ISO 8601
* NULL-terminated string using timezone Z (UTC). If the string fits in
* the space exactly, it leaves out the NULL terminator and returns success.
*
Expand All @@ -580,7 +580,7 @@ int get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base) {
* Returns 0 on success, -1 on failure (for example if the output
* string was too short).
*/
int make_iso_8601_datetime(pandas_datetimestruct *dts, char *outstr, int outlen,
int make_iso_8601_datetime(npy_datetimestruct *dts, char *outstr, int outlen,
NPY_DATETIMEUNIT base) {
char *substr = outstr, sublen = outlen;
int tmplen;
Expand Down
10 changes: 5 additions & 5 deletions pandas/_libs/src/datetime/np_datetime_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ This file implements string parsing and creation for NumPy datetime.
*/
int
parse_iso_8601_datetime(char *str, int len,
pandas_datetimestruct *out,
int *out_local,
int *out_tzoffset);
npy_datetimestruct *out,
int *out_local,
int *out_tzoffset);

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

/*
* Converts an pandas_datetimestruct to an (almost) ISO 8601
* Converts an npy_datetimestruct to an (almost) ISO 8601
* NULL-terminated string using timezone Z (UTC).
*
* 'base' restricts the output to that unit. Set 'base' to
Expand All @@ -73,7 +73,7 @@ get_datetime_iso_8601_strlen(int local, NPY_DATETIMEUNIT base);
* string was too short).
*/
int
make_iso_8601_datetime(pandas_datetimestruct *dts, char *outstr, int outlen,
make_iso_8601_datetime(npy_datetimestruct *dts, char *outstr, int outlen,
NPY_DATETIMEUNIT base);

#endif // PANDAS__LIBS_SRC_DATETIME_NP_DATETIME_STRINGS_H_
24 changes: 12 additions & 12 deletions pandas/_libs/src/period_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ static int monthToQuarter(int month) { return ((month - 1) / 3) + 1; }
* Assumes GREGORIAN_CALENDAR */
npy_int64 unix_date_from_ymd(int year, int month, int day) {
/* Calculate the absolute date */
pandas_datetimestruct dts;
npy_datetimestruct dts;
npy_int64 unix_date;

memset(&dts, 0, sizeof(pandas_datetimestruct));
memset(&dts, 0, sizeof(npy_datetimestruct));
dts.year = year;
dts.month = month;
dts.day = day;
unix_date = pandas_datetimestruct_to_datetime(NPY_FR_D, &dts);
unix_date = npy_datetimestruct_to_datetime(NPY_FR_D, &dts);
return unix_date;
}

Expand Down Expand Up @@ -127,7 +127,7 @@ static npy_int64 DtoB_weekday(npy_int64 unix_date) {
return floordiv(unix_date + 4, 7) * 5 + mod_compat(unix_date + 4, 7) - 4;
}

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

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

static npy_int64 asfreq_DTtoA(npy_int64 ordinal, asfreq_info *af_info) {
pandas_datetimestruct dts;
npy_datetimestruct dts;
ordinal = downsample_daytime(ordinal, af_info);
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts);
if (dts.month > af_info->to_end) {
Expand All @@ -160,7 +160,7 @@ static npy_int64 asfreq_DTtoA(npy_int64 ordinal, asfreq_info *af_info) {
}

static int DtoQ_yq(npy_int64 ordinal, asfreq_info *af_info, int *year) {
pandas_datetimestruct dts;
npy_datetimestruct dts;
int quarter;

pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts);
Expand Down Expand Up @@ -188,7 +188,7 @@ static npy_int64 asfreq_DTtoQ(npy_int64 ordinal, asfreq_info *af_info) {
}

static npy_int64 asfreq_DTtoM(npy_int64 ordinal, asfreq_info *af_info) {
pandas_datetimestruct dts;
npy_datetimestruct dts;

ordinal = downsample_daytime(ordinal, af_info);

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

static npy_int64 asfreq_DTtoB(npy_int64 ordinal, asfreq_info *af_info) {
int roll_back;
pandas_datetimestruct dts;
npy_datetimestruct dts;
npy_int64 unix_date = downsample_daytime(ordinal, af_info);
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);

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

static npy_int64 asfreq_WtoB(npy_int64 ordinal, asfreq_info *af_info) {
int roll_back;
pandas_datetimestruct dts;
npy_datetimestruct dts;
npy_int64 unix_date = asfreq_WtoDT(ordinal, af_info);

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

static npy_int64 asfreq_MtoB(npy_int64 ordinal, asfreq_info *af_info) {
int roll_back;
pandas_datetimestruct dts;
npy_datetimestruct dts;
npy_int64 unix_date = asfreq_MtoDT(ordinal, af_info);

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

static npy_int64 asfreq_QtoB(npy_int64 ordinal, asfreq_info *af_info) {
int roll_back;
pandas_datetimestruct dts;
npy_datetimestruct dts;
npy_int64 unix_date = asfreq_QtoDT(ordinal, af_info);

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

static npy_int64 asfreq_AtoB(npy_int64 ordinal, asfreq_info *af_info) {
int roll_back;
pandas_datetimestruct dts;
npy_datetimestruct dts;
npy_int64 unix_date = asfreq_AtoDT(ordinal, af_info);

pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts);
Expand Down
Loading