-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Implement npy_dtime.pyx #17805
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
Implement npy_dtime.pyx #17805
Changes from 12 commits
53ec4d1
67b3ec3
76cad9a
ce7197d
a1f8fc1
e4ce186
a345836
c0b399e
1791389
16603d4
09a6f19
c05cec7
c0b8216
a9ca0b4
c3a1ce9
7ac9615
e017a52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# cython: profile=False | ||
|
||
from numpy cimport int64_t, int32_t | ||
|
||
|
||
cdef extern from "../src/datetime/np_datetime.h": | ||
ctypedef struct pandas_datetimestruct: | ||
int64_t year | ||
int32_t month, day, hour, min, sec, us, ps, as | ||
|
||
|
||
cdef void check_dts_bounds(pandas_datetimestruct *dts) | ||
|
||
cdef int64_t dtstruct_to_dt64(pandas_datetimestruct* dts) nogil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where these nogil before? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The analogous src/datetime functions are, yes. |
||
cdef void dt64_to_dtstruct(int64_t dt64, pandas_datetimestruct* out) nogil |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
# cython: profile=False | ||
|
||
from numpy cimport int64_t | ||
|
||
cdef extern from "numpy/ndarrayobject.h": | ||
ctypedef int64_t npy_timedelta | ||
ctypedef int64_t npy_datetime | ||
|
||
cdef extern from "../src/datetime/np_datetime.h": | ||
ctypedef enum PANDAS_DATETIMEUNIT: | ||
PANDAS_FR_Y | ||
PANDAS_FR_M | ||
PANDAS_FR_W | ||
PANDAS_FR_D | ||
PANDAS_FR_B | ||
PANDAS_FR_h | ||
PANDAS_FR_m | ||
PANDAS_FR_s | ||
PANDAS_FR_ms | ||
PANDAS_FR_us | ||
PANDAS_FR_ns | ||
PANDAS_FR_ps | ||
PANDAS_FR_fs | ||
PANDAS_FR_as | ||
|
||
int cmp_pandas_datetimestruct(pandas_datetimestruct *a, | ||
pandas_datetimestruct *b) | ||
|
||
npy_datetime pandas_datetimestruct_to_datetime(PANDAS_DATETIMEUNIT fr, | ||
pandas_datetimestruct *d | ||
) nogil | ||
|
||
void pandas_datetime_to_datetimestruct(npy_datetime val, | ||
PANDAS_DATETIMEUNIT fr, | ||
pandas_datetimestruct *result) nogil | ||
|
||
pandas_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS | ||
|
||
# ---------------------------------------------------------------------- | ||
|
||
|
||
class OutOfBoundsDatetime(ValueError): | ||
pass | ||
|
||
|
||
cdef inline void check_dts_bounds(pandas_datetimestruct *dts): | ||
"""Returns True if an error needs to be raised""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a doc-string, and change this (which is wrong), it will just raise if there is an oob date |
||
cdef: | ||
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 OutOfBoundsDatetime( | ||
'Out of bounds nanosecond timestamp: %s' % fmt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use {} in formatting. I would also pass dts to the OutOfBounds Constructor and format it there (a bit more idiomatic I think). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now I'll implement that half of this that I know how to do; will work on my .format-fu and follow-up with the rest. |
||
|
||
|
||
# ---------------------------------------------------------------------- | ||
# Conversion | ||
|
||
cdef inline int64_t dtstruct_to_dt64(pandas_datetimestruct* dts) nogil: | ||
"""Convenience function to call pandas_datetimestruct_to_datetime | ||
with the by-far-most-common frequency PANDAS_FR_ns""" | ||
return pandas_datetimestruct_to_datetime(PANDAS_FR_ns, dts) | ||
|
||
|
||
cdef inline void dt64_to_dtstruct(int64_t dt64, | ||
pandas_datetimestruct* out) nogil: | ||
"""Convenience function to call pandas_datetime_to_datetimestruct | ||
with the by-far-most-common frequency PANDAS_FR_ns""" | ||
pandas_datetime_to_datetimestruct(dt64, PANDAS_FR_ns, out) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could actually just call this module util i think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to avoid name overlap with the existing
libs/src/util
module. Also because in the dev branch I've ported src/util to a pure-cython (no C deps--> much simpler setup.py)tslibs.util
. Don't want to get those mixed up.My first choice is still the original
npy_dtime
, sincenp_datetime
overlaps with the existinglibs/src/datetime/np_datetime
files.