diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 9fbe717fa8c2c..6005905ad59fd 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -32,6 +32,7 @@ from pandas._libs.tslibs.np_datetime cimport ( npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct) from pandas._libs.tslibs.timezones import UTC from pandas._libs.tslibs.tzconversion cimport tz_convert_single +from pandas._libs.tslibs.c_timestamp cimport _Timestamp # --------------------------------------------------------------------- @@ -105,17 +106,18 @@ cdef to_offset(object obj): return to_offset(obj) -def as_datetime(obj): - f = getattr(obj, 'to_pydatetime', None) - if f is not None: - obj = f() +def as_datetime(obj: datetime) -> datetime: + if isinstance(obj, _Timestamp): + return obj.to_pydatetime() return obj -cpdef bint is_normalized(dt): - if (dt.hour != 0 or dt.minute != 0 or dt.second != 0 or - dt.microsecond != 0 or getattr(dt, 'nanosecond', 0) != 0): +cpdef bint is_normalized(datetime dt): + if dt.hour != 0 or dt.minute != 0 or dt.second != 0 or dt.microsecond != 0: + # Regardless of whether dt is datetime vs Timestamp return False + if isinstance(dt, _Timestamp): + return dt.nanosecond == 0 return True