Skip to content

TYP: annotations, typing for infer_tzinfo #35084

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 2 commits 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
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,12 @@ def datetime_to_datetime64(ndarray[object] values):
"""
cdef:
Py_ssize_t i, n = len(values)
object val, inferred_tz = None
object val
int64_t[:] iresult
npy_datetimestruct dts
_TSObject _ts
bint found_naive = False
tzinfo inferred_tz = None

result = np.empty(n, dtype='M8[ns]')
iresult = result.view('i8')
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timezone
from cpython.datetime cimport tzinfo, PyTZInfo_Check, PyDateTime_IMPORT
from cpython.datetime cimport datetime, tzinfo, PyTZInfo_Check, PyDateTime_IMPORT
PyDateTime_IMPORT

# dateutil compat
Expand Down Expand Up @@ -286,7 +286,7 @@ cdef object get_dst_info(tzinfo tz):
return dst_cache[cache_key]


def infer_tzinfo(start, end):
def infer_tzinfo(datetime start, datetime end):
if start is not None and end is not None:
tz = start.tzinfo
if not tz_compare(tz, end.tzinfo):
Expand Down
13 changes: 8 additions & 5 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, time, timedelta
from datetime import datetime, time, timedelta, tzinfo
from typing import Optional, Union
import warnings

Expand Down Expand Up @@ -1908,6 +1908,7 @@ def sequence_to_dt64ns(
inferred_freq = None

dtype = _validate_dt64_dtype(dtype)
tz = timezones.maybe_get_tz(tz)

if not hasattr(data, "dtype"):
# e.g. list, tuple
Expand Down Expand Up @@ -1950,14 +1951,14 @@ def sequence_to_dt64ns(
data, inferred_tz = objects_to_datetime64ns(
data, dayfirst=dayfirst, yearfirst=yearfirst
)
tz = maybe_infer_tz(tz, inferred_tz)
tz = _maybe_infer_tz(tz, inferred_tz)
data_dtype = data.dtype

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

elif is_datetime64_dtype(data_dtype):
Expand Down Expand Up @@ -2144,7 +2145,9 @@ def maybe_convert_dtype(data, copy):
# Validation and Inference


def maybe_infer_tz(tz, inferred_tz):
def _maybe_infer_tz(
tz: Optional[tzinfo], inferred_tz: Optional[tzinfo]
) -> Optional[tzinfo]:
"""
If a timezone is inferred from data, check that it is compatible with
the user-provided timezone, if any.
Expand Down Expand Up @@ -2216,7 +2219,7 @@ def _validate_dt64_dtype(dtype):
return dtype


def validate_tz_from_dtype(dtype, tz):
def validate_tz_from_dtype(dtype, tz: Optional[tzinfo]) -> Optional[tzinfo]:
"""
If the given dtype is a DatetimeTZDtype, extract the implied
tzinfo object from it and check that it does not conflict with the given
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4714,7 +4714,7 @@ def _set_tz(
if tz is not None:
name = getattr(values, "name", None)
values = values.ravel()
tz = timezones.get_timezone(_ensure_decoded(tz))
tz = _ensure_decoded(tz)
values = DatetimeIndex(values, name=name)
values = values.tz_localize("UTC").tz_convert(tz)
elif coerce:
Expand Down