Skip to content

REF: avoid parse_datetime_string in tslib #50808

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 2 commits into from
Jan 18, 2023
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
35 changes: 14 additions & 21 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ from pandas._libs.tslibs.np_datetime cimport (
npy_datetimestruct_to_datetime,
pandas_datetime_to_datetimestruct,
pydate_to_dt64,
pydatetime_to_dt64,
string_to_dts,
)
from pandas._libs.tslibs.strptime cimport parse_today_now
Expand All @@ -46,7 +45,6 @@ from pandas._libs.util cimport (
)

from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
from pandas._libs.tslibs.parsing import parse_datetime_string

from pandas._libs.tslibs.conversion cimport (
_TSObject,
Expand Down Expand Up @@ -542,13 +540,7 @@ cpdef array_to_datetime(
_ts = convert_str_to_tsobject(
val, None, unit="ns", dayfirst=dayfirst, yearfirst=yearfirst
)
try:
_ts.ensure_reso(NPY_FR_ns)
except OutOfBoundsDatetime as err:
# re-raise with better exception message
raise OutOfBoundsDatetime(
f"Out of bounds nanosecond timestamp: {val}"
) from err
_ts.ensure_reso(NPY_FR_ns, val)

iresult[i] = _ts.value

Expand Down Expand Up @@ -698,6 +690,7 @@ cdef _array_to_datetime_object(
ndarray[object] oresult
npy_datetimestruct dts
cnp.broadcast mi
_TSObject tsobj

assert is_raise or is_ignore or is_coerce

Expand Down Expand Up @@ -725,20 +718,20 @@ cdef _array_to_datetime_object(
oresult[i] = "NaT"
cnp.PyArray_MultiIter_NEXT(mi)
continue
elif val == "now":
oresult[i] = datetime.now()
cnp.PyArray_MultiIter_NEXT(mi)
continue
elif val == "today":
oresult[i] = datetime.today()
cnp.PyArray_MultiIter_NEXT(mi)
continue

try:
oresult[i] = parse_datetime_string(val, dayfirst=dayfirst,
yearfirst=yearfirst)
pydatetime_to_dt64(oresult[i], &dts)
check_dts_bounds(&dts)
tsobj = convert_str_to_tsobject(
val, None, unit="ns", dayfirst=dayfirst, yearfirst=yearfirst
)
tsobj.ensure_reso(NPY_FR_ns, val)

dts = tsobj.dts
oresult[i] = datetime(
dts.year, dts.month, dts.day, dts.hour, dts.min, dts.sec, dts.us,
tzinfo=tsobj.tzinfo,
fold=tsobj.fold,
)

except (ValueError, OverflowError) as ex:
ex.args = (f"{ex}, at position {i}", )
if is_coerce:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/conversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cdef class _TSObject:
bint fold
NPY_DATETIMEUNIT creso

cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso) except? -1
cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso, str val=*) except? -1


cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
Expand Down
6 changes: 5 additions & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,15 @@ cdef class _TSObject:
self.fold = 0
self.creso = NPY_FR_ns # default value

cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso) except? -1:
cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso, str val=None) except? -1:
if self.creso != creso:
try:
self.value = convert_reso(self.value, self.creso, creso, False)
except OverflowError as err:
if val is not None:
raise OutOfBoundsDatetime(
f"Out of bounds nanosecond timestamp: {val}"
) from err
raise OutOfBoundsDatetime from err

self.creso = creso
Expand Down