Skip to content

Commit

Permalink
TYP: misc typing cleanup in core/indexes/multi.py (pandas-dev#36007)
Browse files Browse the repository at this point in the history
* TYP: misc typing cleanup in core/indexes/multi.py

* update per comments
  • Loading branch information
simonjayhawkins authored and Kevin D Smith committed Nov 2, 2020
1 parent ad32f71 commit 8dd5983
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from pandas._libs import algos as libalgos, index as libindex, lib
from pandas._libs.hashtable import duplicated_int64
from pandas._typing import AnyArrayLike, Scalar
from pandas._typing import AnyArrayLike, Label, Scalar
from pandas.compat.numpy import function as nv
from pandas.errors import InvalidIndexError, PerformanceWarning, UnsortedIndexError
from pandas.util._decorators import Appender, cache_readonly, doc
Expand Down Expand Up @@ -449,7 +449,12 @@ def from_arrays(cls, arrays, sortorder=None, names=lib.no_default) -> "MultiInde
)

@classmethod
def from_tuples(cls, tuples, sortorder=None, names=None):
def from_tuples(
cls,
tuples,
sortorder: Optional[int] = None,
names: Optional[Sequence[Label]] = None,
):
"""
Convert list of tuples to MultiIndex.
Expand Down Expand Up @@ -490,6 +495,7 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
elif is_iterator(tuples):
tuples = list(tuples)

arrays: List[Sequence[Label]]
if len(tuples) == 0:
if names is None:
raise TypeError("Cannot infer number of levels from empty list")
Expand Down Expand Up @@ -700,8 +706,13 @@ def levels(self):
return FrozenList(result)

def _set_levels(
self, levels, level=None, copy=False, validate=True, verify_integrity=False
):
self,
levels,
level=None,
copy: bool = False,
validate: bool = True,
verify_integrity: bool = False,
) -> None:
# This is NOT part of the levels property because it should be
# externally not allowed to set levels. User beware if you change
# _levels directly
Expand All @@ -719,10 +730,10 @@ def _set_levels(
)
else:
level_numbers = [self._get_level_number(lev) for lev in level]
new_levels = list(self._levels)
new_levels_list = list(self._levels)
for lev_num, lev in zip(level_numbers, levels):
new_levels[lev_num] = ensure_index(lev, copy=copy)._shallow_copy()
new_levels = FrozenList(new_levels)
new_levels_list[lev_num] = ensure_index(lev, copy=copy)._shallow_copy()
new_levels = FrozenList(new_levels_list)

if verify_integrity:
new_codes = self._verify_integrity(levels=new_levels)
Expand Down Expand Up @@ -875,8 +886,13 @@ def codes(self):
return self._codes

def _set_codes(
self, codes, level=None, copy=False, validate=True, verify_integrity=False
):
self,
codes,
level=None,
copy: bool = False,
validate: bool = True,
verify_integrity: bool = False,
) -> None:
if validate:
if level is None and len(codes) != self.nlevels:
raise ValueError("Length of codes must match number of levels")
Expand All @@ -890,11 +906,13 @@ def _set_codes(
)
else:
level_numbers = [self._get_level_number(lev) for lev in level]
new_codes = list(self._codes)
new_codes_list = list(self._codes)
for lev_num, level_codes in zip(level_numbers, codes):
lev = self.levels[lev_num]
new_codes[lev_num] = _coerce_indexer_frozen(level_codes, lev, copy=copy)
new_codes = FrozenList(new_codes)
new_codes_list[lev_num] = _coerce_indexer_frozen(
level_codes, lev, copy=copy
)
new_codes = FrozenList(new_codes_list)

if verify_integrity:
new_codes = self._verify_integrity(codes=new_codes)
Expand Down Expand Up @@ -2435,7 +2453,7 @@ def _get_partial_string_timestamp_match_key(self, key):
if isinstance(key, str) and self.levels[0]._supports_partial_string_indexing:
# Convert key '2016-01-01' to
# ('2016-01-01'[, slice(None, None, None)]+)
key = tuple([key] + [slice(None)] * (len(self.levels) - 1))
key = (key,) + (slice(None),) * (len(self.levels) - 1)

if isinstance(key, tuple):
# Convert (..., '2016-01-01', ...) in tuple to
Expand Down Expand Up @@ -3086,7 +3104,7 @@ def _update_indexer(idxr, indexer=indexer):
elif is_list_like(k):
# a collection of labels to include from this level (these
# are or'd)
indexers = None
indexers: Optional[Int64Index] = None
for x in k:
try:
idxrs = _convert_to_indexer(
Expand Down

0 comments on commit 8dd5983

Please sign in to comment.