-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Move time conversion funcs to tslibs.conversion #17708
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 all commits
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 |
---|---|---|
|
@@ -45,8 +45,6 @@ cdef double NaN = <double> np.NaN | |
cdef double nan = NaN | ||
cdef double NAN = nan | ||
|
||
from datetime import datetime as pydatetime | ||
|
||
# this is our tseries.pxd | ||
from datetime cimport ( | ||
get_timedelta64_value, get_datetime64_value, | ||
|
@@ -134,58 +132,10 @@ def memory_usage_of_objects(ndarray[object, ndim=1] arr): | |
|
||
#---------------------------------------------------------------------- | ||
# datetime / io related | ||
|
||
cdef int _EPOCH_ORD = 719163 | ||
|
||
from datetime import date as pydate | ||
|
||
cdef inline int64_t gmtime(object date): | ||
cdef int y, m, d, h, mn, s, days | ||
|
||
y = PyDateTime_GET_YEAR(date) | ||
m = PyDateTime_GET_MONTH(date) | ||
d = PyDateTime_GET_DAY(date) | ||
h = PyDateTime_DATE_GET_HOUR(date) | ||
mn = PyDateTime_DATE_GET_MINUTE(date) | ||
s = PyDateTime_DATE_GET_SECOND(date) | ||
|
||
days = pydate(y, m, 1).toordinal() - _EPOCH_ORD + d - 1 | ||
return ((<int64_t> (((days * 24 + h) * 60 + mn))) * 60 + s) * 1000 | ||
|
||
|
||
cpdef object to_datetime(int64_t timestamp): | ||
return pydatetime.utcfromtimestamp(timestamp / 1000.0) | ||
|
||
|
||
cpdef object to_timestamp(object dt): | ||
return gmtime(dt) | ||
|
||
|
||
def array_to_timestamp(ndarray[object, ndim=1] arr): | ||
cdef int i, n | ||
cdef ndarray[int64_t, ndim=1] result | ||
|
||
n = len(arr) | ||
result = np.empty(n, dtype=np.int64) | ||
|
||
for i from 0 <= i < n: | ||
result[i] = gmtime(arr[i]) | ||
|
||
return result | ||
|
||
|
||
def time64_to_datetime(ndarray[int64_t, ndim=1] arr): | ||
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. this is just a simple call to 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. This is only used in 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. yes, I think you can just replace that call in pytables |
||
cdef int i, n | ||
cdef ndarray[object, ndim=1] result | ||
|
||
n = len(arr) | ||
result = np.empty(n, dtype=object) | ||
|
||
for i from 0 <= i < n: | ||
result[i] = to_datetime(arr[i]) | ||
|
||
return result | ||
|
||
from tslibs.conversion import ( # noqa | ||
time64_to_datetime, | ||
array_to_timestamp, to_timestamp, to_datetime) | ||
from tslibs.conversion cimport to_timestamp, to_datetime, gmtime # noqa | ||
|
||
#---------------------------------------------------------------------- | ||
# isnull / notnull related | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
# cython: profile=False | ||
|
||
from numpy cimport int64_t | ||
|
||
cdef int64_t gmtime(object date) | ||
cpdef object to_datetime(int64_t timestamp) | ||
cpdef object to_timestamp(object dt) | ||
|
||
cdef _to_i8(object val) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- coding: utf-8 -*- | ||
# cython: profile=False | ||
|
||
|
||
from datetime import ( | ||
date as pydate, | ||
datetime as pydatetime) | ||
|
||
from cpython.datetime cimport ( | ||
PyDateTime_Check, | ||
PyDateTime_GET_YEAR, | ||
PyDateTime_GET_MONTH, | ||
PyDateTime_GET_DAY, | ||
PyDateTime_DATE_GET_HOUR, | ||
PyDateTime_DATE_GET_MINUTE, | ||
PyDateTime_DATE_GET_SECOND, | ||
PyDateTime_IMPORT) | ||
PyDateTime_IMPORT | ||
|
||
from datetime cimport (get_datetime64_value, _pydatetime_to_dts, | ||
pandas_datetimestruct) | ||
|
||
import numpy as np | ||
cimport numpy as cnp | ||
from numpy cimport int64_t, ndarray | ||
cnp.import_array() | ||
|
||
cimport util | ||
|
||
from timezones cimport get_utcoffset, is_utc | ||
|
||
# ---------------------------------------------------------------------- | ||
# Constants | ||
cdef int _EPOCH_ORD = 719163 | ||
|
||
# ---------------------------------------------------------------------- | ||
# Non-pandas-specific | ||
|
||
cpdef object to_datetime(int64_t timestamp): | ||
return pydatetime.utcfromtimestamp(timestamp / 1000.0) | ||
|
||
|
||
cdef inline int64_t gmtime(object date): | ||
cdef int y, m, d, h, mn, s, days | ||
|
||
y = PyDateTime_GET_YEAR(date) | ||
m = PyDateTime_GET_MONTH(date) | ||
d = PyDateTime_GET_DAY(date) | ||
h = PyDateTime_DATE_GET_HOUR(date) | ||
mn = PyDateTime_DATE_GET_MINUTE(date) | ||
s = PyDateTime_DATE_GET_SECOND(date) | ||
|
||
days = pydate(y, m, 1).toordinal() - _EPOCH_ORD + d - 1 | ||
return ((<int64_t> (((days * 24 + h) * 60 + mn))) * 60 + s) * 1000 | ||
|
||
|
||
cpdef object to_timestamp(object dt): | ||
return gmtime(dt) | ||
|
||
|
||
def array_to_timestamp(ndarray[object, ndim=1] arr): | ||
cdef int i, n | ||
cdef ndarray[int64_t, ndim=1] result | ||
|
||
n = len(arr) | ||
result = np.empty(n, dtype=np.int64) | ||
|
||
for i in range(n): | ||
result[i] = gmtime(arr[i]) | ||
|
||
return result | ||
|
||
|
||
def time64_to_datetime(ndarray[int64_t, ndim=1] arr): | ||
cdef int i, n | ||
cdef ndarray[object, ndim=1] result | ||
|
||
n = len(arr) | ||
result = np.empty(n, dtype=object) | ||
|
||
for i in range(n): | ||
result[i] = to_datetime(arr[i]) | ||
|
||
return result | ||
|
||
|
||
# ---------------------------------------------------------------------- | ||
|
||
cdef inline _to_i8(object val): | ||
cdef pandas_datetimestruct dts | ||
try: | ||
return val.value | ||
except AttributeError: | ||
if util.is_datetime64_object(val): | ||
return get_datetime64_value(val) | ||
elif PyDateTime_Check(val): | ||
tzinfo = getattr(val, 'tzinfo', None) | ||
# Save the original date value so we can get the utcoffset from it. | ||
ival = _pydatetime_to_dts(val, &dts) | ||
if tzinfo is not None and not is_utc(tzinfo): | ||
offset = get_utcoffset(tzinfo, val) | ||
ival -= int(offset.total_seconds() * 1e9) | ||
return ival | ||
return val |
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 don't think this is called anywhere?
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.
Doesn't look like it, no. But its
def
and notcdef
, so conceivably used downstream. OK to remove?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.
yes ok to remove
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.
these are ALL private modules (did that on purpose), so can move code around as long as we are internally consistent