Skip to content

Commit 940ea7a

Browse files
authored
REF: add creso keyword to parse_pydatetime (#55579)
1 parent 864596b commit 940ea7a

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

pandas/_libs/tslib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ cpdef array_to_datetime(
456456
tzinfo tz_out = None
457457
bint found_tz = False, found_naive = False
458458
cnp.flatiter it = cnp.PyArray_IterNew(values)
459+
NPY_DATETIMEUNIT creso = NPY_FR_ns
459460

460461
# specify error conditions
461462
assert is_raise or is_ignore or is_coerce
@@ -484,7 +485,7 @@ cpdef array_to_datetime(
484485
found_tz,
485486
utc_convert,
486487
)
487-
iresult[i] = parse_pydatetime(val, &dts, utc_convert)
488+
iresult[i] = parse_pydatetime(val, &dts, utc_convert, creso=creso)
488489

489490
elif PyDate_Check(val):
490491
iresult[i] = pydate_to_dt64(val, &dts)

pandas/_libs/tslibs/conversion.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ cdef int64_t parse_pydatetime(
5959
datetime val,
6060
npy_datetimestruct *dts,
6161
bint utc_convert,
62+
NPY_DATETIMEUNIT creso,
6263
) except? -1

pandas/_libs/tslibs/conversion.pyx

+12-10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ from pandas._libs.tslibs.nattype cimport (
6161
c_nat_strings as nat_strings,
6262
)
6363
from pandas._libs.tslibs.parsing cimport parse_datetime_string
64+
from pandas._libs.tslibs.timestamps cimport _Timestamp
6465
from pandas._libs.tslibs.timezones cimport (
6566
get_utcoffset,
6667
is_utc,
@@ -302,8 +303,8 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
302303
pandas_datetime_to_datetimestruct(ts, NPY_FR_ns, &obj.dts)
303304
elif PyDateTime_Check(ts):
304305
if nanos == 0:
305-
if isinstance(ts, ABCTimestamp):
306-
reso = abbrev_to_npy_unit(ts.unit) # TODO: faster way to do this?
306+
if isinstance(ts, _Timestamp):
307+
reso = (<_Timestamp>ts)._creso
307308
else:
308309
# TODO: what if user explicitly passes nanos=0?
309310
reso = NPY_FR_us
@@ -729,6 +730,7 @@ cdef int64_t parse_pydatetime(
729730
datetime val,
730731
npy_datetimestruct *dts,
731732
bint utc_convert,
733+
NPY_DATETIMEUNIT creso,
732734
) except? -1:
733735
"""
734736
Convert pydatetime to datetime64.
@@ -741,6 +743,8 @@ cdef int64_t parse_pydatetime(
741743
Needed to use in pydatetime_to_dt64, which writes to it.
742744
utc_convert : bool
743745
Whether to convert/localize to UTC.
746+
creso : NPY_DATETIMEUNIT
747+
Resolution to store the the result.
744748
745749
Raises
746750
------
@@ -752,17 +756,15 @@ cdef int64_t parse_pydatetime(
752756

753757
if val.tzinfo is not None:
754758
if utc_convert:
755-
_ts = convert_datetime_to_tsobject(val, None)
756-
_ts.ensure_reso(NPY_FR_ns)
759+
_ts = convert_datetime_to_tsobject(val, None, nanos=0, reso=creso)
757760
result = _ts.value
758761
else:
759-
_ts = convert_datetime_to_tsobject(val, None)
760-
_ts.ensure_reso(NPY_FR_ns)
762+
_ts = convert_datetime_to_tsobject(val, None, nanos=0, reso=creso)
761763
result = _ts.value
762764
else:
763-
if isinstance(val, ABCTimestamp):
764-
result = val.as_unit("ns")._value
765+
if isinstance(val, _Timestamp):
766+
result = (<_Timestamp>val)._as_creso(creso, round_ok=False)._value
765767
else:
766-
result = pydatetime_to_dt64(val, dts)
767-
check_dts_bounds(dts)
768+
result = pydatetime_to_dt64(val, dts, reso=creso)
769+
check_dts_bounds(dts, creso)
768770
return result

0 commit comments

Comments
 (0)