Skip to content

REF: turn _try_mi into MultiIndex._get_values_for_loc #31736

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 4 commits into from
Feb 9, 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
6 changes: 4 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4601,7 +4601,7 @@ def get_value(self, series: "Series", key):
else:
raise

return self._get_values_for_loc(series, loc)
return self._get_values_for_loc(series, loc, key)

def _should_fallback_to_positional(self) -> bool:
"""
Expand All @@ -4611,12 +4611,14 @@ def _should_fallback_to_positional(self) -> bool:
return False
return True

def _get_values_for_loc(self, series: "Series", loc):
def _get_values_for_loc(self, series: "Series", loc, key):
"""
Do a positional lookup on the given Series, returning either a scalar
or a Series.

Assumes that `series.index is self`

key is included for MultiIndex compat.
"""
if is_integer(loc):
return series._values[loc]
Expand Down
51 changes: 34 additions & 17 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from sys import getsizeof
from typing import Any, Hashable, Iterable, List, Optional, Sequence, Tuple, Union
from typing import (
TYPE_CHECKING,
Any,
Hashable,
Iterable,
List,
Optional,
Sequence,
Tuple,
Union,
)
import warnings

import numpy as np
Expand Down Expand Up @@ -56,6 +66,9 @@
pprint_thing,
)

if TYPE_CHECKING:
from pandas import Series # noqa:F401

_index_doc_kwargs = dict(ibase._index_doc_kwargs)
_index_doc_kwargs.update(
dict(klass="MultiIndex", target_klass="MultiIndex or list of tuples")
Expand Down Expand Up @@ -2326,28 +2339,32 @@ def get_value(self, series, key):
# We have to explicitly exclude generators, as these are hashable.
raise InvalidIndexError(key)

def _try_mi(k):
# TODO: what if a level contains tuples??
loc = self.get_loc(k)

new_values = series._values[loc]
if is_scalar(loc):
return new_values

new_index = self[loc]
new_index = maybe_droplevels(new_index, k)
return series._constructor(
new_values, index=new_index, name=series.name
).__finalize__(self)

try:
return _try_mi(key)
loc = self.get_loc(key)
except KeyError:
if is_integer(key):
return series._values[key]
loc = key
else:
raise

return self._get_values_for_loc(series, loc, key)

def _get_values_for_loc(self, series: "Series", loc, key):
"""
Do a positional lookup on the given Series, returning either a scalar
or a Series.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename to _get_values_for_positional_loc to make this extra clear (I know for indexes we only have the concept of positional indexing, but still). follown ok.

also pls type / doc-string as much as possible

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_values_for_positional_loc

good idea, will follow up


Assumes that `series.index is self`
"""
new_values = series._values[loc]
if is_scalar(loc):
return new_values

new_index = self[loc]
new_index = maybe_droplevels(new_index, key)
new_ser = series._constructor(new_values, index=new_index, name=series.name)
return new_ser.__finalize__(series)

def _convert_listlike_indexer(self, keyarr):
"""
Parameters
Expand Down