Skip to content

Separate out strptime.pyx from tslib #17342

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 16 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions pandas/_libs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from .tslib import iNaT, NaT, Timestamp, Timedelta, OutOfBoundsDatetime
Expand Down
21 changes: 21 additions & 0 deletions pandas/_libs/src/datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ cdef extern from "datetime/np_datetime.h":
PANDAS_DATETIMEUNIT fr,
pandas_datetimestruct *result) nogil
int days_per_month_table[2][12]
pandas_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS

int dayofweek(int y, int m, int d) nogil
int is_leapyear(int64_t year) nogil
Expand Down Expand Up @@ -161,3 +162,23 @@ cdef inline int64_t _date_to_datetime64(object val,
dts.hour = dts.min = dts.sec = dts.us = 0
dts.ps = dts.as = 0
return pandas_datetimestruct_to_datetime(PANDAS_FR_ns, dts)


cdef inline check_dts_bounds(pandas_datetimestruct *dts):
cdef:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to make this a bint, and just return True/False.

Then make another function which calls this one which actually raises the error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The longer-term solution that I've implemented but not PRed is to move a bunch of datetime.pxd into a pyx file where OutOfBoundsDatetime can be defined. This also ends up clearing out a lot of the setup.py dependencies (orthogonal to this discussion, but still).

Is returning a bint actually any simpler than re-raising? It isn't obvious whether True means "error state is True" or "everything is OK is True", whereas raising is unambiguous.

That said, I'll do it your way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah its ok, just don't like to muddle even more.

bint error = False

if (dts.year <= 1677 and
cmp_pandas_datetimestruct(dts, &_NS_MIN_DTS) == -1):
error = True
elif (dts.year >= 2262 and
cmp_pandas_datetimestruct(dts, &_NS_MAX_DTS) == 1):
error = True

if error:
fmt = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year, dts.month,
dts.day, dts.hour,
dts.min, dts.sec)

raise ValueError(
'Out of bounds nanosecond timestamp: %s' % fmt)
6 changes: 6 additions & 0 deletions pandas/_libs/src/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
#endif

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


const int days_per_month_table[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/src/datetime/np_datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ typedef struct {
int num;
} pandas_datetime_metadata;

extern const pandas_datetimestruct _NS_MIN_DTS;
extern const pandas_datetimestruct _NS_MAX_DTS;

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

Expand Down
Loading