Skip to content

Commit 7483414

Browse files
authored
REF: avoid parse_datetime_string in tslib (#50808)
* REF: avoid parse_datetime_string in tslib * de-dup now/today handling
1 parent d2d1797 commit 7483414

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

pandas/_libs/tslib.pyx

+14-21
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ from pandas._libs.tslibs.np_datetime cimport (
3535
npy_datetimestruct_to_datetime,
3636
pandas_datetime_to_datetimestruct,
3737
pydate_to_dt64,
38-
pydatetime_to_dt64,
3938
string_to_dts,
4039
)
4140
from pandas._libs.tslibs.strptime cimport parse_today_now
@@ -46,7 +45,6 @@ from pandas._libs.util cimport (
4645
)
4746

4847
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
49-
from pandas._libs.tslibs.parsing import parse_datetime_string
5048

5149
from pandas._libs.tslibs.conversion cimport (
5250
_TSObject,
@@ -542,13 +540,7 @@ cpdef array_to_datetime(
542540
_ts = convert_str_to_tsobject(
543541
val, None, unit="ns", dayfirst=dayfirst, yearfirst=yearfirst
544542
)
545-
try:
546-
_ts.ensure_reso(NPY_FR_ns)
547-
except OutOfBoundsDatetime as err:
548-
# re-raise with better exception message
549-
raise OutOfBoundsDatetime(
550-
f"Out of bounds nanosecond timestamp: {val}"
551-
) from err
543+
_ts.ensure_reso(NPY_FR_ns, val)
552544

553545
iresult[i] = _ts.value
554546

@@ -698,6 +690,7 @@ cdef _array_to_datetime_object(
698690
ndarray[object] oresult
699691
npy_datetimestruct dts
700692
cnp.broadcast mi
693+
_TSObject tsobj
701694

702695
assert is_raise or is_ignore or is_coerce
703696

@@ -725,20 +718,20 @@ cdef _array_to_datetime_object(
725718
oresult[i] = "NaT"
726719
cnp.PyArray_MultiIter_NEXT(mi)
727720
continue
728-
elif val == "now":
729-
oresult[i] = datetime.now()
730-
cnp.PyArray_MultiIter_NEXT(mi)
731-
continue
732-
elif val == "today":
733-
oresult[i] = datetime.today()
734-
cnp.PyArray_MultiIter_NEXT(mi)
735-
continue
736721

737722
try:
738-
oresult[i] = parse_datetime_string(val, dayfirst=dayfirst,
739-
yearfirst=yearfirst)
740-
pydatetime_to_dt64(oresult[i], &dts)
741-
check_dts_bounds(&dts)
723+
tsobj = convert_str_to_tsobject(
724+
val, None, unit="ns", dayfirst=dayfirst, yearfirst=yearfirst
725+
)
726+
tsobj.ensure_reso(NPY_FR_ns, val)
727+
728+
dts = tsobj.dts
729+
oresult[i] = datetime(
730+
dts.year, dts.month, dts.day, dts.hour, dts.min, dts.sec, dts.us,
731+
tzinfo=tsobj.tzinfo,
732+
fold=tsobj.fold,
733+
)
734+
742735
except (ValueError, OverflowError) as ex:
743736
ex.args = (f"{ex}, at position {i}", )
744737
if is_coerce:

pandas/_libs/tslibs/conversion.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cdef class _TSObject:
2424
bint fold
2525
NPY_DATETIMEUNIT creso
2626

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

2929

3030
cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,

pandas/_libs/tslibs/conversion.pyx

+5-1
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,15 @@ cdef class _TSObject:
212212
self.fold = 0
213213
self.creso = NPY_FR_ns # default value
214214

215-
cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso) except? -1:
215+
cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso, str val=None) except? -1:
216216
if self.creso != creso:
217217
try:
218218
self.value = convert_reso(self.value, self.creso, creso, False)
219219
except OverflowError as err:
220+
if val is not None:
221+
raise OutOfBoundsDatetime(
222+
f"Out of bounds nanosecond timestamp: {val}"
223+
) from err
220224
raise OutOfBoundsDatetime from err
221225

222226
self.creso = creso

0 commit comments

Comments
 (0)