Skip to content

Commit eb4bbe2

Browse files
authored
CLN: stronger typing in libtimezones (#35082)
1 parent c616c68 commit eb4bbe2

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ class Timestamp(_Timestamp):
991991
"Timestamp from components."
992992
)
993993

994-
if tz is not None and treat_tz_as_pytz(tz):
994+
if tz is not None and PyTZInfo_Check(tz) and treat_tz_as_pytz(tz):
995995
raise ValueError(
996996
"pytz timezones do not support fold. Please use dateutil "
997997
"timezones."

pandas/_libs/tslibs/timezones.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ from cpython.datetime cimport tzinfo
22

33
cdef tzinfo utc_pytz
44

5-
cpdef bint is_utc(object tz)
6-
cdef bint is_tzlocal(object tz)
5+
cpdef bint is_utc(tzinfo tz)
6+
cdef bint is_tzlocal(tzinfo tz)
77

8-
cdef bint treat_tz_as_pytz(object tz)
8+
cdef bint treat_tz_as_pytz(tzinfo tz)
99

1010
cpdef bint tz_compare(object start, object end)
1111
cpdef object get_timezone(object tz)

pandas/_libs/tslibs/timezones.pyx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from cpython.datetime cimport tzinfo
21
from datetime import timezone
2+
from cpython.datetime cimport tzinfo, PyTZInfo_Check, PyDateTime_IMPORT
3+
PyDateTime_IMPORT
34

45
# dateutil compat
56
from dateutil.tz import (
@@ -29,20 +30,20 @@ cdef tzinfo utc_pytz = UTC
2930

3031
# ----------------------------------------------------------------------
3132

32-
cpdef inline bint is_utc(object tz):
33+
cpdef inline bint is_utc(tzinfo tz):
3334
return tz is utc_pytz or tz is utc_stdlib or isinstance(tz, _dateutil_tzutc)
3435

3536

36-
cdef inline bint is_tzlocal(object tz):
37+
cdef inline bint is_tzlocal(tzinfo tz):
3738
return isinstance(tz, _dateutil_tzlocal)
3839

3940

40-
cdef inline bint treat_tz_as_pytz(object tz):
41+
cdef inline bint treat_tz_as_pytz(tzinfo tz):
4142
return (hasattr(tz, '_utc_transition_times') and
4243
hasattr(tz, '_transition_info'))
4344

4445

45-
cdef inline bint treat_tz_as_dateutil(object tz):
46+
cdef inline bint treat_tz_as_dateutil(tzinfo tz):
4647
return hasattr(tz, '_trans_list') and hasattr(tz, '_trans_idx')
4748

4849

@@ -59,7 +60,9 @@ cpdef inline object get_timezone(object tz):
5960
the tz name. It needs to be a string so that we can serialize it with
6061
UJSON/pytables. maybe_get_tz (below) is the inverse of this process.
6162
"""
62-
if is_utc(tz):
63+
if not PyTZInfo_Check(tz):
64+
return tz
65+
elif is_utc(tz):
6366
return tz
6467
else:
6568
if treat_tz_as_dateutil(tz):
@@ -327,7 +330,7 @@ cpdef bint tz_compare(object start, object end):
327330
return get_timezone(start) == get_timezone(end)
328331

329332

330-
def tz_standardize(tz: object):
333+
def tz_standardize(tz: tzinfo):
331334
"""
332335
If the passed tz is a pytz timezone object, "normalize" it to the a
333336
consistent version

0 commit comments

Comments
 (0)