@@ -61,6 +61,7 @@ from pandas._libs.tslibs.nattype cimport (
61
61
c_nat_strings as nat_strings,
62
62
)
63
63
from pandas._libs.tslibs.parsing cimport parse_datetime_string
64
+ from pandas._libs.tslibs.timestamps cimport _Timestamp
64
65
from pandas._libs.tslibs.timezones cimport (
65
66
get_utcoffset,
66
67
is_utc,
@@ -302,8 +303,8 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
302
303
pandas_datetime_to_datetimestruct(ts, NPY_FR_ns, & obj.dts)
303
304
elif PyDateTime_Check(ts):
304
305
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
307
308
else :
308
309
# TODO: what if user explicitly passes nanos=0?
309
310
reso = NPY_FR_us
@@ -729,6 +730,7 @@ cdef int64_t parse_pydatetime(
729
730
datetime val,
730
731
npy_datetimestruct * dts,
731
732
bint utc_convert,
733
+ NPY_DATETIMEUNIT creso,
732
734
) except ? - 1 :
733
735
"""
734
736
Convert pydatetime to datetime64.
@@ -741,6 +743,8 @@ cdef int64_t parse_pydatetime(
741
743
Needed to use in pydatetime_to_dt64, which writes to it.
742
744
utc_convert : bool
743
745
Whether to convert/localize to UTC.
746
+ creso : NPY_DATETIMEUNIT
747
+ Resolution to store the the result.
744
748
745
749
Raises
746
750
------
@@ -752,17 +756,15 @@ cdef int64_t parse_pydatetime(
752
756
753
757
if val.tzinfo is not None :
754
758
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)
757
760
result = _ts.value
758
761
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)
761
763
result = _ts.value
762
764
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
765
767
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 )
768
770
return result
0 commit comments