Skip to content

Commit 8d10bfb

Browse files
authored
TYP: annotations, typing for infer_tzinfo (#35084)
1 parent 5507452 commit 8d10bfb

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

pandas/_libs/tslibs/conversion.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@ def datetime_to_datetime64(ndarray[object] values):
246246
"""
247247
cdef:
248248
Py_ssize_t i, n = len(values)
249-
object val, inferred_tz = None
249+
object val
250250
int64_t[:] iresult
251251
npy_datetimestruct dts
252252
_TSObject _ts
253253
bint found_naive = False
254+
tzinfo inferred_tz = None
254255

255256
result = np.empty(n, dtype='M8[ns]')
256257
iresult = result.view('i8')

pandas/_libs/tslibs/timezones.pyx

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

55
# dateutil compat
@@ -286,7 +286,7 @@ cdef object get_dst_info(tzinfo tz):
286286
return dst_cache[cache_key]
287287

288288

289-
def infer_tzinfo(start, end):
289+
def infer_tzinfo(datetime start, datetime end):
290290
if start is not None and end is not None:
291291
tz = start.tzinfo
292292
if not tz_compare(tz, end.tzinfo):

pandas/core/arrays/datetimes.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, time, timedelta
1+
from datetime import datetime, time, timedelta, tzinfo
22
from typing import Optional, Union
33
import warnings
44

@@ -1908,6 +1908,7 @@ def sequence_to_dt64ns(
19081908
inferred_freq = None
19091909

19101910
dtype = _validate_dt64_dtype(dtype)
1911+
tz = timezones.maybe_get_tz(tz)
19111912

19121913
if not hasattr(data, "dtype"):
19131914
# e.g. list, tuple
@@ -1950,14 +1951,14 @@ def sequence_to_dt64ns(
19501951
data, inferred_tz = objects_to_datetime64ns(
19511952
data, dayfirst=dayfirst, yearfirst=yearfirst
19521953
)
1953-
tz = maybe_infer_tz(tz, inferred_tz)
1954+
tz = _maybe_infer_tz(tz, inferred_tz)
19541955
data_dtype = data.dtype
19551956

19561957
# `data` may have originally been a Categorical[datetime64[ns, tz]],
19571958
# so we need to handle these types.
19581959
if is_datetime64tz_dtype(data_dtype):
19591960
# DatetimeArray -> ndarray
1960-
tz = maybe_infer_tz(tz, data.tz)
1961+
tz = _maybe_infer_tz(tz, data.tz)
19611962
result = data._data
19621963

19631964
elif is_datetime64_dtype(data_dtype):
@@ -2144,7 +2145,9 @@ def maybe_convert_dtype(data, copy):
21442145
# Validation and Inference
21452146

21462147

2147-
def maybe_infer_tz(tz, inferred_tz):
2148+
def _maybe_infer_tz(
2149+
tz: Optional[tzinfo], inferred_tz: Optional[tzinfo]
2150+
) -> Optional[tzinfo]:
21482151
"""
21492152
If a timezone is inferred from data, check that it is compatible with
21502153
the user-provided timezone, if any.
@@ -2216,7 +2219,7 @@ def _validate_dt64_dtype(dtype):
22162219
return dtype
22172220

22182221

2219-
def validate_tz_from_dtype(dtype, tz):
2222+
def validate_tz_from_dtype(dtype, tz: Optional[tzinfo]) -> Optional[tzinfo]:
22202223
"""
22212224
If the given dtype is a DatetimeTZDtype, extract the implied
22222225
tzinfo object from it and check that it does not conflict with the given

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4714,7 +4714,7 @@ def _set_tz(
47144714
if tz is not None:
47154715
name = getattr(values, "name", None)
47164716
values = values.ravel()
4717-
tz = timezones.get_timezone(_ensure_decoded(tz))
4717+
tz = _ensure_decoded(tz)
47184718
values = DatetimeIndex(values, name=name)
47194719
values = values.tz_localize("UTC").tz_convert(tz)
47204720
elif coerce:

0 commit comments

Comments
 (0)