-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Move remaining conversion functions to tslibs.conversion #18358
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 4 commits
3e986f1
08236be
f19eeba
e156b3f
0ef759f
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 |
---|---|---|
|
@@ -40,7 +40,7 @@ from timezones cimport ( | |
from parsing import parse_datetime_string | ||
|
||
from nattype import nat_strings, NaT | ||
from nattype cimport NPY_NAT | ||
from nattype cimport NPY_NAT, _checknull_with_nat | ||
|
||
# ---------------------------------------------------------------------- | ||
# Constants | ||
|
@@ -73,6 +73,123 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1: | |
|
||
return ival | ||
|
||
|
||
def cast_to_nanoseconds(ndarray 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. let's rename this maybe |
||
""" | ||
Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]' | ||
|
||
Parameters | ||
---------- | ||
arr : ndarray | ||
|
||
Returns | ||
------- | ||
result : ndarray with dtype datetime64[ns] | ||
|
||
""" | ||
cdef: | ||
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 doc-string |
||
Py_ssize_t i, n = arr.size | ||
ndarray[int64_t] ivalues, iresult | ||
PANDAS_DATETIMEUNIT unit | ||
pandas_datetimestruct dts | ||
|
||
shape = (<object> arr).shape | ||
|
||
ivalues = arr.view(np.int64).ravel() | ||
|
||
result = np.empty(shape, dtype='M8[ns]') | ||
iresult = result.ravel().view(np.int64) | ||
|
||
if len(iresult) == 0: | ||
return result | ||
|
||
unit = get_datetime64_unit(arr.flat[0]) | ||
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. prob should add some checks around this, IOW this could already be ns (TODO ok) 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. Yah, this check is done elsewhere, will not be hard to implement. If it weren't for |
||
for i in range(n): | ||
if ivalues[i] != NPY_NAT: | ||
pandas_datetime_to_datetimestruct(ivalues[i], unit, &dts) | ||
iresult[i] = dtstruct_to_dt64(&dts) | ||
check_dts_bounds(&dts) | ||
else: | ||
iresult[i] = NPY_NAT | ||
|
||
return result | ||
|
||
|
||
def datetime_to_datetime64(ndarray[object] values): | ||
""" | ||
Convert ndarray of datetime-like objects to int64 array representing | ||
nanosecond timestamps. | ||
|
||
Parameters | ||
---------- | ||
values : ndarray | ||
|
||
Returns | ||
------- | ||
result : ndarray witth dtype int64 | ||
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. typo |
||
inferred_tz : tzinfo or None | ||
""" | ||
cdef: | ||
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. same |
||
Py_ssize_t i, n = len(values) | ||
object val, inferred_tz = None | ||
ndarray[int64_t] iresult | ||
pandas_datetimestruct dts | ||
_TSObject _ts | ||
|
||
result = np.empty(n, dtype='M8[ns]') | ||
iresult = result.view('i8') | ||
for i in range(n): | ||
val = values[i] | ||
if _checknull_with_nat(val): | ||
iresult[i] = NPY_NAT | ||
elif PyDateTime_Check(val): | ||
if val.tzinfo is not None: | ||
if inferred_tz is not None: | ||
if get_timezone(val.tzinfo) != inferred_tz: | ||
raise ValueError('Array must be all same time zone') | ||
else: | ||
inferred_tz = get_timezone(val.tzinfo) | ||
|
||
_ts = convert_datetime_to_tsobject(val, None) | ||
iresult[i] = _ts.value | ||
check_dts_bounds(&_ts.dts) | ||
else: | ||
if inferred_tz is not None: | ||
raise ValueError('Cannot mix tz-aware with ' | ||
'tz-naive values') | ||
iresult[i] = pydatetime_to_dt64(val, &dts) | ||
check_dts_bounds(&dts) | ||
else: | ||
raise TypeError('Unrecognized value type: %s' % type(val)) | ||
|
||
return result, inferred_tz | ||
|
||
|
||
cdef inline _maybe_datetimelike_to_i8(object val): | ||
""" | ||
Try to convert to a nanosecond timestamp. Fall back to returning the | ||
input value. | ||
|
||
Parameters | ||
---------- | ||
val : object | ||
|
||
Returns | ||
------- | ||
val : int64 timestamp or original input | ||
""" | ||
cdef: | ||
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. doc-string. can you rename this to something else. maybe |
||
pandas_datetimestruct dts | ||
try: | ||
return val.value | ||
except AttributeError: | ||
if is_datetime64_object(val): | ||
return get_datetime64_value(val) | ||
elif PyDateTime_Check(val): | ||
return convert_datetime_to_tsobject(val, None).value | ||
return val | ||
|
||
|
||
# ---------------------------------------------------------------------- | ||
# _TSObject Conversion | ||
|
||
|
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.
note to de-private this at some point as well