Skip to content
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

TYP: DatetimeIndex, TimedeltaIndex #37365

Merged
merged 6 commits into from
Oct 29, 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
27 changes: 6 additions & 21 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down