Skip to content

Commit 0698dd8

Browse files
authored
CLN: avoid getattr checks in liboffsets (#34008)
1 parent 78af0d3 commit 0698dd8

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

pandas/_libs/tslibs/offsets.pyx

+9-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ from pandas._libs.tslibs.np_datetime cimport (
3232
npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct)
3333
from pandas._libs.tslibs.timezones import UTC
3434
from pandas._libs.tslibs.tzconversion cimport tz_convert_single
35+
from pandas._libs.tslibs.c_timestamp cimport _Timestamp
3536

3637

3738
# ---------------------------------------------------------------------
@@ -105,17 +106,18 @@ cdef to_offset(object obj):
105106
return to_offset(obj)
106107

107108

108-
def as_datetime(obj):
109-
f = getattr(obj, 'to_pydatetime', None)
110-
if f is not None:
111-
obj = f()
109+
def as_datetime(obj: datetime) -> datetime:
110+
if isinstance(obj, _Timestamp):
111+
return obj.to_pydatetime()
112112
return obj
113113

114114

115-
cpdef bint is_normalized(dt):
116-
if (dt.hour != 0 or dt.minute != 0 or dt.second != 0 or
117-
dt.microsecond != 0 or getattr(dt, 'nanosecond', 0) != 0):
115+
cpdef bint is_normalized(datetime dt):
116+
if dt.hour != 0 or dt.minute != 0 or dt.second != 0 or dt.microsecond != 0:
117+
# Regardless of whether dt is datetime vs Timestamp
118118
return False
119+
if isinstance(dt, _Timestamp):
120+
return dt.nanosecond == 0
119121
return True
120122

121123

0 commit comments

Comments
 (0)