From b3af87008e4055b908b7322ada00c0e2ec1e043b Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 23 Oct 2020 08:55:16 -0700 Subject: [PATCH 1/4] TYP: DatetimeIndex, TimedeltaIndex --- pandas/core/indexes/datetimelike.py | 23 ++++------------------- pandas/core/indexes/datetimes.py | 25 ++++++++++++++++++++++++- pandas/io/parsers.py | 2 +- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 76d001d2f3ce6..e72888dc40905 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 datetime import datetime from typing import TYPE_CHECKING, Any, List, Optional, 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 @@ -932,21 +930,8 @@ def join( ) 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 + # 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..90c7afac47803 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -6,7 +6,13 @@ 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,23 @@ def union_many(self, others): return this.rename(res_name) return this + def _maybe_utc_convert(self, other): + 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") + else: + assert False + 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) From 569ddefe4c3b554551f5188e12d65e496658834e Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 23 Oct 2020 09:04:19 -0700 Subject: [PATCH 2/4] CLN: remove assertion --- pandas/core/indexes/datetimes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 90c7afac47803..43f031daac841 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -430,8 +430,6 @@ def _maybe_utc_convert(self, other): if not timezones.tz_compare(self.tz, other.tz): this = self.tz_convert("UTC") other = other.tz_convert("UTC") - else: - assert False return this, other # -------------------------------------------------------------------- From 3509b3465ea8b6d75417faff5da0b39756048603 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 26 Oct 2020 08:07:29 -0700 Subject: [PATCH 3/4] annotate --- pandas/core/indexes/datetimelike.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index cbdaf80b4afcc..2c422b4c551b6 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -2,7 +2,7 @@ Base and utility classes for tseries type pandas objects. """ from datetime import datetime -from typing import TYPE_CHECKING, Any, List, Optional, TypeVar, Union, cast +from typing import TYPE_CHECKING, Any, List, Optional, Tuple, TypeVar, Union, cast import numpy as np @@ -929,7 +929,7 @@ def join( sort=sort, ) - def _maybe_utc_convert(self, other): + def _maybe_utc_convert(self: _T, other: Index) -> Tuple[_T, Index]: # Overridden by DatetimeIndex return self, other From 1921e3139bc0228e70e9e5d39a042845e7be18ea Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 26 Oct 2020 10:27:03 -0700 Subject: [PATCH 4/4] annotate --- pandas/core/indexes/datetimes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 43f031daac841..ce6f2cbdf5eaa 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1,6 +1,6 @@ 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 @@ -417,7 +417,7 @@ def union_many(self, others): return this.rename(res_name) return this - def _maybe_utc_convert(self, other): + def _maybe_utc_convert(self, other: Index) -> Tuple["DatetimeIndex", Index]: this = self if isinstance(other, DatetimeIndex):