-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Implement numpy_helper functions directly in cython #18059
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
Changes from 2 commits
c8f7158
ea4bef0
0b87865
387bb44
13b85d8
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 |
---|---|---|
|
@@ -5,12 +5,67 @@ from cpython.datetime cimport date, datetime | |
|
||
from numpy cimport int64_t, int32_t | ||
|
||
cdef extern from "numpy/ndarrayobject.h": | ||
ctypedef int64_t npy_timedelta | ||
ctypedef int64_t npy_datetime | ||
|
||
cdef extern from "numpy/ndarraytypes.h": | ||
# ctypedef struct npy_datetimestruct: | ||
# int64_t year | ||
# int32_t month, day, hour, min, sec, us, ps, as | ||
|
||
ctypedef enum NPY_DATETIMEUNIT: | ||
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. Is this enum needed? 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. No, it's leftover from an earlier revision. Will remove. |
||
NPY_FR_Y = 0 # Years | ||
NPY_FR_M = 1 # Months | ||
NPY_FR_W = 2 # Weeks | ||
# Gap where 1.6 NPY_FR_B (value 3) was | ||
NPY_FR_D = 4 # Days | ||
NPY_FR_h = 5 # hours | ||
NPY_FR_m = 6 # minutes | ||
NPY_FR_s = 7 # seconds | ||
NPY_FR_ms = 8 # milliseconds | ||
NPY_FR_us = 9 # microseconds | ||
NPY_FR_ns = 10 # nanoseconds | ||
NPY_FR_ps = 11 # picoseconds | ||
NPY_FR_fs = 12 # femtoseconds | ||
NPY_FR_as = 13 # attoseconds | ||
NPY_FR_GENERIC = 14 # Generic, unbound units, can convert to anything | ||
|
||
ctypedef struct PyArray_DatetimeMetaData: | ||
PANDAS_DATETIMEUNIT base | ||
int64_t num | ||
|
||
cdef extern from "numpy/arrayscalars.h": | ||
ctypedef struct PyDatetimeScalarObject: | ||
# PyObject_HEAD | ||
npy_datetime obval | ||
PyArray_DatetimeMetaData obmeta | ||
|
||
ctypedef struct PyTimedeltaScalarObject: | ||
# PyObject_HEAD | ||
npy_timedelta obval | ||
PyArray_DatetimeMetaData obmeta | ||
|
||
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 | ||
|
||
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 | ||
|
||
cdef check_dts_bounds(pandas_datetimestruct *dts) | ||
|
||
|
@@ -19,3 +74,7 @@ cdef void dt64_to_dtstruct(int64_t dt64, pandas_datetimestruct* out) nogil | |
|
||
cdef int64_t pydatetime_to_dt64(datetime val, pandas_datetimestruct *dts) | ||
cdef int64_t pydate_to_dt64(date val, pandas_datetimestruct *dts) | ||
|
||
cdef npy_datetime get_datetime64_value(object obj) nogil | ||
cdef npy_timedelta get_timedelta64_value(object obj) nogil | ||
cdef PANDAS_DATETIMEUNIT get_datetime64_unit(object obj) nogil |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,27 +12,7 @@ PyDateTime_IMPORT | |
|
||
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) | ||
|
||
|
@@ -47,6 +27,34 @@ cdef extern from "../src/datetime/np_datetime.h": | |
pandas_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS | ||
|
||
# ---------------------------------------------------------------------- | ||
# numpy object inspection | ||
|
||
cdef inline npy_datetime get_datetime64_value(object obj) 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. Can you make the docstrings more descriptive? Something like 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. Good idea, will change. |
||
Adapted from numpy_helper.h version: | ||
|
||
PANDAS_INLINE npy_datetime get_datetime64_value(PyObject* obj) { | ||
return ((PyDatetimeScalarObject*)obj)->obval; | ||
} | ||
""" | ||
return (<PyDatetimeScalarObject*>obj).obval | ||
|
||
|
||
cdef inline npy_timedelta get_timedelta64_value(object obj) nogil: | ||
""" | ||
Adapted from numpy_helper.h version: | ||
|
||
PANDAS_INLINE npy_timedelta get_timedelta64_value(PyObject* obj) { | ||
return ((PyTimedeltaScalarObject*)obj)->obval; | ||
} | ||
""" | ||
return (<PyTimedeltaScalarObject*>obj).obval | ||
|
||
|
||
cdef inline PANDAS_DATETIMEUNIT get_datetime64_unit(object obj) nogil: | ||
return <PANDAS_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base | ||
|
||
# ---------------------------------------------------------------------- | ||
|
||
|
||
class OutOfBoundsDatetime(ValueError): | ||
|
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.
so this now makes lib depend on np_datetime.pxd, which is ok as tseries_depends now lists this.