diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 43252ffb5bf13..55454e6c58755 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -456,6 +456,7 @@ cpdef array_to_datetime( tzinfo tz_out = None bint found_tz = False, found_naive = False cnp.flatiter it = cnp.PyArray_IterNew(values) + NPY_DATETIMEUNIT creso = NPY_FR_ns # specify error conditions assert is_raise or is_ignore or is_coerce @@ -484,7 +485,7 @@ cpdef array_to_datetime( found_tz, utc_convert, ) - iresult[i] = parse_pydatetime(val, &dts, utc_convert) + iresult[i] = parse_pydatetime(val, &dts, utc_convert, creso=creso) elif PyDate_Check(val): iresult[i] = pydate_to_dt64(val, &dts) diff --git a/pandas/_libs/tslibs/conversion.pxd b/pandas/_libs/tslibs/conversion.pxd index 1b3214605582a..3e5a79e833a25 100644 --- a/pandas/_libs/tslibs/conversion.pxd +++ b/pandas/_libs/tslibs/conversion.pxd @@ -59,4 +59,5 @@ cdef int64_t parse_pydatetime( datetime val, npy_datetimestruct *dts, bint utc_convert, + NPY_DATETIMEUNIT creso, ) except? -1 diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 45c4d7809fe7a..1f4bb8af4542f 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -61,6 +61,7 @@ from pandas._libs.tslibs.nattype cimport ( c_nat_strings as nat_strings, ) from pandas._libs.tslibs.parsing cimport parse_datetime_string +from pandas._libs.tslibs.timestamps cimport _Timestamp from pandas._libs.tslibs.timezones cimport ( get_utcoffset, is_utc, @@ -302,8 +303,8 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit, pandas_datetime_to_datetimestruct(ts, NPY_FR_ns, &obj.dts) elif PyDateTime_Check(ts): if nanos == 0: - if isinstance(ts, ABCTimestamp): - reso = abbrev_to_npy_unit(ts.unit) # TODO: faster way to do this? + if isinstance(ts, _Timestamp): + reso = (<_Timestamp>ts)._creso else: # TODO: what if user explicitly passes nanos=0? reso = NPY_FR_us @@ -729,6 +730,7 @@ cdef int64_t parse_pydatetime( datetime val, npy_datetimestruct *dts, bint utc_convert, + NPY_DATETIMEUNIT creso, ) except? -1: """ Convert pydatetime to datetime64. @@ -741,6 +743,8 @@ cdef int64_t parse_pydatetime( Needed to use in pydatetime_to_dt64, which writes to it. utc_convert : bool Whether to convert/localize to UTC. + creso : NPY_DATETIMEUNIT + Resolution to store the the result. Raises ------ @@ -752,17 +756,15 @@ cdef int64_t parse_pydatetime( if val.tzinfo is not None: if utc_convert: - _ts = convert_datetime_to_tsobject(val, None) - _ts.ensure_reso(NPY_FR_ns) + _ts = convert_datetime_to_tsobject(val, None, nanos=0, reso=creso) result = _ts.value else: - _ts = convert_datetime_to_tsobject(val, None) - _ts.ensure_reso(NPY_FR_ns) + _ts = convert_datetime_to_tsobject(val, None, nanos=0, reso=creso) result = _ts.value else: - if isinstance(val, ABCTimestamp): - result = val.as_unit("ns")._value + if isinstance(val, _Timestamp): + result = (<_Timestamp>val)._as_creso(creso, round_ok=False)._value else: - result = pydatetime_to_dt64(val, dts) - check_dts_bounds(dts) + result = pydatetime_to_dt64(val, dts, reso=creso) + check_dts_bounds(dts, creso) return result