Skip to content

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

Merged
merged 5 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
…libs-npy_dtime5

update docstring per reviewer suggestion
  • Loading branch information
jbrockmendel committed Nov 3, 2017
commit 387bb44d87dac299c81bfd9714a726f575b70a4f
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/conversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from cpython.datetime cimport datetime

from numpy cimport int64_t, int32_t

from np_datetime cimport pandas_datetimestruct


Expand Down
21 changes: 0 additions & 21 deletions pandas/_libs/tslibs/np_datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,6 @@ cdef extern from "numpy/ndarrayobject.h":
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:
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
Expand Down
45 changes: 36 additions & 9 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,57 @@ cdef extern from "../src/datetime/np_datetime.h":

cdef inline npy_datetime get_datetime64_value(object obj) nogil:
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you make the docstrings more descriptive? Something like "returns int64 value underlying numpy datetime scalar Python object"

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, will change.

Adapted from numpy_helper.h version:
returns the int64 value underlying scalar numpy datetime64 object

PANDAS_INLINE npy_datetime get_datetime64_value(PyObject* obj) {
return ((PyDatetimeScalarObject*)obj)->obval;
}
Note that to interpret this as a datetime, the corresponding unit is
also needed. That can be found using `get_datetime64_unit`.
"""
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;
}
returns the int64 value underlying scalar numpy timedelta64 object
"""
return (<PyTimedeltaScalarObject*>obj).obval


cdef inline PANDAS_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
"""
returns the unit part of the dtype for a numpy datetime64 object.
"""
return <PANDAS_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base

# ----------------------------------------------------------------------
# Comparison

cdef int reverse_ops[6]

reverse_ops[Py_LT] = Py_GT
reverse_ops[Py_LE] = Py_GE
reverse_ops[Py_EQ] = Py_EQ
reverse_ops[Py_NE] = Py_NE
reverse_ops[Py_GT] = Py_LT
reverse_ops[Py_GE] = Py_LE


cdef inline bint cmp_scalar(int64_t lhs, int64_t rhs, int op) except -1:
"""
cmp_scalar is a more performant version of PyObject_RichCompare
typed for int64_t arguments.
"""
if op == Py_EQ:
return lhs == rhs
elif op == Py_NE:
return lhs != rhs
elif op == Py_LT:
return lhs < rhs
elif op == Py_LE:
return lhs <= rhs
elif op == Py_GT:
return lhs > rhs
elif op == Py_GE:
return lhs >= rhs


class OutOfBoundsDatetime(ValueError):
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.