Skip to content

Commit ebca2a6

Browse files
authored
TYP: maybe_get_tz (pandas-dev#35103)
1 parent 3db2d02 commit ebca2a6

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

pandas/_libs/tslibs/timezones.pxd

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

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

1414
cdef timedelta get_utcoffset(tzinfo tz, datetime obj)
1515
cdef bint is_fixed_offset(tzinfo tz)

pandas/_libs/tslibs/timezones.pyx

+7-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ cpdef inline object get_timezone(tzinfo tz):
8484
return tz
8585

8686

87-
cpdef inline object maybe_get_tz(object tz):
87+
cpdef inline tzinfo maybe_get_tz(object tz):
8888
"""
8989
(Maybe) Construct a timezone object from a string. If tz is a string, use
9090
it to construct a timezone object. Otherwise, just return tz.
@@ -102,6 +102,12 @@ cpdef inline object maybe_get_tz(object tz):
102102
tz = pytz.timezone(tz)
103103
elif is_integer_object(tz):
104104
tz = pytz.FixedOffset(tz / 60)
105+
elif isinstance(tz, tzinfo):
106+
pass
107+
elif tz is None:
108+
pass
109+
else:
110+
raise TypeError(type(tz))
105111
return tz
106112

107113

pandas/tests/tslibs/test_timezones.py

+12
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,15 @@ def test_infer_tz_mismatch(infer_setup, ordered):
106106

107107
with pytest.raises(AssertionError, match=msg):
108108
timezones.infer_tzinfo(*args)
109+
110+
111+
def test_maybe_get_tz_invalid_types():
112+
with pytest.raises(TypeError, match="<class 'float'>"):
113+
timezones.maybe_get_tz(44.0)
114+
115+
with pytest.raises(TypeError, match="<class 'module'>"):
116+
timezones.maybe_get_tz(pytz)
117+
118+
msg = "<class 'pandas._libs.tslibs.timestamps.Timestamp'>"
119+
with pytest.raises(TypeError, match=msg):
120+
timezones.maybe_get_tz(Timestamp.now("UTC"))

0 commit comments

Comments
 (0)