diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 863880e222b5d..2c422b4c551b6 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -1,13 +1,13 @@ """ Base and utility classes for tseries type pandas objects. """ -from datetime import datetime, tzinfo -from typing import TYPE_CHECKING, Any, List, Optional, TypeVar, Union, cast +from datetime import datetime +from typing import TYPE_CHECKING, Any, List, Optional, Tuple, TypeVar, Union, cast import numpy as np from pandas._libs import NaT, Timedelta, iNaT, join as libjoin, lib -from pandas._libs.tslibs import BaseOffset, Resolution, Tick, timezones +from pandas._libs.tslibs import BaseOffset, Resolution, Tick from pandas._typing import Callable, Label from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -672,8 +672,6 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index): but not PeriodIndex """ - tz: Optional[tzinfo] - # Compat for frequency inference, see GH#23789 _is_monotonic_increasing = Index.is_monotonic_increasing _is_monotonic_decreasing = Index.is_monotonic_decreasing @@ -931,22 +929,9 @@ def join( sort=sort, ) - def _maybe_utc_convert(self, other): - this = self - if not hasattr(self, "tz"): - return this, other - - if isinstance(other, type(self)): - if self.tz is not None: - if other.tz is None: - raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex") - elif other.tz is not None: - raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex") - - if not timezones.tz_compare(self.tz, other.tz): - this = self.tz_convert("UTC") - other = other.tz_convert("UTC") - return this, other + def _maybe_utc_convert(self: _T, other: Index) -> Tuple[_T, Index]: + # Overridden by DatetimeIndex + return self, other # -------------------------------------------------------------------- # List-Like Methods diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 11417e0b3317e..ce6f2cbdf5eaa 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1,12 +1,18 @@ from datetime import date, datetime, time, timedelta, tzinfo import operator -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Optional, Tuple import warnings import numpy as np from pandas._libs import NaT, Period, Timestamp, index as libindex, lib -from pandas._libs.tslibs import Resolution, ints_to_pydatetime, parsing, to_offset +from pandas._libs.tslibs import ( + Resolution, + ints_to_pydatetime, + parsing, + timezones, + to_offset, +) from pandas._libs.tslibs.offsets import prefix_mapping from pandas._typing import DtypeObj, Label from pandas.errors import InvalidIndexError @@ -411,6 +417,21 @@ def union_many(self, others): return this.rename(res_name) return this + def _maybe_utc_convert(self, other: Index) -> Tuple["DatetimeIndex", Index]: + this = self + + if isinstance(other, DatetimeIndex): + if self.tz is not None: + if other.tz is None: + raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex") + elif other.tz is not None: + raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex") + + if not timezones.tz_compare(self.tz, other.tz): + this = self.tz_convert("UTC") + other = other.tz_convert("UTC") + return this, other + # -------------------------------------------------------------------- def _get_time_micros(self): diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 20c0297448494..2110a2d400be8 100644 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -2177,7 +2177,7 @@ def TextParser(*args, **kwds): return TextFileReader(*args, **kwds) -def count_empty_vals(vals): +def count_empty_vals(vals) -> int: return sum(1 for v in vals if v == "" or v is None)