Skip to content

TYP: type for get_timezone #35096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timezones.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cdef bint is_tzlocal(tzinfo tz)
cdef bint treat_tz_as_pytz(tzinfo tz)

cpdef bint tz_compare(tzinfo start, tzinfo end)
cpdef object get_timezone(object tz)
cpdef object get_timezone(tzinfo tz)
cpdef object maybe_get_tz(object tz)

cdef get_utcoffset(tzinfo tz, obj)
Expand Down
9 changes: 3 additions & 6 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import timezone
from cpython.datetime cimport datetime, tzinfo, PyTZInfo_Check, PyDateTime_IMPORT
PyDateTime_IMPORT
from cpython.datetime cimport datetime, tzinfo

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


cpdef inline object get_timezone(object tz):
cpdef inline object get_timezone(tzinfo tz):
"""
We need to do several things here:
1) Distinguish between pytz and dateutil timezones
Expand All @@ -60,9 +59,7 @@ cpdef inline object get_timezone(object tz):
the tz name. It needs to be a string so that we can serialize it with
UJSON/pytables. maybe_get_tz (below) is the inverse of this process.
"""
if not PyTZInfo_Check(tz):
return tz
elif is_utc(tz):
if is_utc(tz):
return tz
else:
if treat_tz_as_dateutil(tz):
Expand Down
17 changes: 6 additions & 11 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,6 @@ def tzinfo(self):
"""
return self.tz

@property # NB: override with cache_readonly in immutable subclasses
def _timezone(self):
"""
Comparable timezone both for pytz / dateutil
"""
return timezones.get_timezone(self.tzinfo)

@property # NB: override with cache_readonly in immutable subclasses
def is_normalized(self):
"""
Expand Down Expand Up @@ -617,15 +610,17 @@ def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
# -----------------------------------------------------------------
# Comparison Methods

def _has_same_tz(self, other):
zzone = self._timezone
def _has_same_tz(self, other) -> bool:

# vzone shouldn't be None if value is non-datetime like
if isinstance(other, np.datetime64):
# convert to Timestamp as np.datetime64 doesn't have tz attr
other = Timestamp(other)
vzone = timezones.get_timezone(getattr(other, "tzinfo", "__no_tz__"))
return zzone == vzone

if not hasattr(other, "tzinfo"):
return False
other_tz = other.tzinfo
return timezones.tz_compare(self.tzinfo, other_tz)

def _assert_tzawareness_compat(self, other):
# adapted from _Timestamp._assert_tzawareness_compat
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def _new_DatetimeIndex(cls, d):
DatetimeArray,
wrap=True,
)
@inherit_names(
["_timezone", "is_normalized", "_resolution_obj"], DatetimeArray, cache=True
)
@inherit_names(["is_normalized", "_resolution_obj"], DatetimeArray, cache=True)
@inherit_names(
[
"_bool_ops",
Expand Down