Skip to content

Commit a4e19fa

Browse files
authored
TYP: type for get_timezone (#35096)
1 parent 7412c5a commit a4e19fa

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

pandas/_libs/tslibs/timezones.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cdef bint is_tzlocal(tzinfo tz)
88
cdef bint treat_tz_as_pytz(tzinfo tz)
99

1010
cpdef bint tz_compare(tzinfo start, tzinfo end)
11-
cpdef object get_timezone(object tz)
11+
cpdef object get_timezone(tzinfo tz)
1212
cpdef object maybe_get_tz(object tz)
1313

1414
cdef get_utcoffset(tzinfo tz, obj)

pandas/_libs/tslibs/timezones.pyx

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import timezone
2-
from cpython.datetime cimport datetime, tzinfo, PyTZInfo_Check, PyDateTime_IMPORT
3-
PyDateTime_IMPORT
2+
from cpython.datetime cimport datetime, tzinfo
43

54
# dateutil compat
65
from dateutil.tz import (
@@ -47,7 +46,7 @@ cdef inline bint treat_tz_as_dateutil(tzinfo tz):
4746
return hasattr(tz, '_trans_list') and hasattr(tz, '_trans_idx')
4847

4948

50-
cpdef inline object get_timezone(object tz):
49+
cpdef inline object get_timezone(tzinfo tz):
5150
"""
5251
We need to do several things here:
5352
1) Distinguish between pytz and dateutil timezones
@@ -60,9 +59,7 @@ cpdef inline object get_timezone(object tz):
6059
the tz name. It needs to be a string so that we can serialize it with
6160
UJSON/pytables. maybe_get_tz (below) is the inverse of this process.
6261
"""
63-
if not PyTZInfo_Check(tz):
64-
return tz
65-
elif is_utc(tz):
62+
if is_utc(tz):
6663
return tz
6764
else:
6865
if treat_tz_as_dateutil(tz):

pandas/core/arrays/datetimes.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,6 @@ def tzinfo(self):
522522
"""
523523
return self.tz
524524

525-
@property # NB: override with cache_readonly in immutable subclasses
526-
def _timezone(self):
527-
"""
528-
Comparable timezone both for pytz / dateutil
529-
"""
530-
return timezones.get_timezone(self.tzinfo)
531-
532525
@property # NB: override with cache_readonly in immutable subclasses
533526
def is_normalized(self):
534527
"""
@@ -617,15 +610,17 @@ def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
617610
# -----------------------------------------------------------------
618611
# Comparison Methods
619612

620-
def _has_same_tz(self, other):
621-
zzone = self._timezone
613+
def _has_same_tz(self, other) -> bool:
622614

623615
# vzone shouldn't be None if value is non-datetime like
624616
if isinstance(other, np.datetime64):
625617
# convert to Timestamp as np.datetime64 doesn't have tz attr
626618
other = Timestamp(other)
627-
vzone = timezones.get_timezone(getattr(other, "tzinfo", "__no_tz__"))
628-
return zzone == vzone
619+
620+
if not hasattr(other, "tzinfo"):
621+
return False
622+
other_tz = other.tzinfo
623+
return timezones.tz_compare(self.tzinfo, other_tz)
629624

630625
def _assert_tzawareness_compat(self, other):
631626
# adapted from _Timestamp._assert_tzawareness_compat

pandas/core/indexes/datetimes.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ def _new_DatetimeIndex(cls, d):
7474
DatetimeArray,
7575
wrap=True,
7676
)
77-
@inherit_names(
78-
["_timezone", "is_normalized", "_resolution_obj"], DatetimeArray, cache=True
79-
)
77+
@inherit_names(["is_normalized", "_resolution_obj"], DatetimeArray, cache=True)
8078
@inherit_names(
8179
[
8280
"_bool_ops",

0 commit comments

Comments
 (0)