Skip to content

PERF: do NPY_NAT check inside get_datetime64_nanos #24031

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 1 commit into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 7 additions & 10 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,13 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',

elif is_datetime64_object(val):
seen_datetime = 1
if get_datetime64_value(val) == NPY_NAT:
iresult[i] = NPY_NAT
else:
try:
iresult[i] = get_datetime64_nanos(val)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
try:
iresult[i] = get_datetime64_nanos(val)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise

elif is_integer_object(val) or is_float_object(val):
# these must be ns unit by-definition
Expand Down
11 changes: 6 additions & 5 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
NPY_DATETIMEUNIT unit
npy_datetime ival

unit = get_datetime64_unit(val)
ival = get_datetime64_value(val)
if ival == NPY_NAT:
return NPY_NAT
Copy link
Contributor

Choose a reason for hiding this comment

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

does this hit a currently tested case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Within tests/indexes/datetimes there are 14


unit = get_datetime64_unit(val)

if unit != NPY_FR_ns:
pandas_datetime_to_datetimestruct(ival, unit, &dts)
Expand Down Expand Up @@ -283,10 +286,8 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
if ts is None or ts is NaT:
obj.value = NPY_NAT
elif is_datetime64_object(ts):
if ts.view('i8') == NPY_NAT:
obj.value = NPY_NAT
else:
obj.value = get_datetime64_nanos(ts)
obj.value = get_datetime64_nanos(ts)
if obj.value != NPY_NAT:
dt64_to_dtstruct(obj.value, &obj.dts)
elif is_integer_object(ts):
if ts == NPY_NAT:
Expand Down