Skip to content

REF: Simplify __getitem__ by doing positional-int check first #33471

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 1 commit into from
Apr 12, 2020
Merged
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
41 changes: 22 additions & 19 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,32 +881,35 @@ def __getitem__(self, key):
if isinstance(key, (list, tuple)):
key = unpack_1tuple(key)

if key_is_scalar or isinstance(self.index, MultiIndex):
if is_integer(key) and self.index._should_fallback_to_positional():
return self._values[key]

elif key_is_scalar:
return self._get_value(key)

if (
isinstance(key, tuple)
and is_hashable(key)
and isinstance(self.index, MultiIndex)
):
# Otherwise index.get_value will raise InvalidIndexError
try:
result = self.index.get_value(self, key)
result = self._get_value(key)

return result
except InvalidIndexError:
if not isinstance(self.index, MultiIndex):
raise

except (KeyError, ValueError):
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
# kludge
pass
else:
raise
except KeyError:
# We still have the corner case where this tuple is a key
# in the first level of our MultiIndex
return self._get_values_tuple(key)

if not key_is_scalar:
# avoid expensive checks if we know we have a scalar
if is_iterator(key):
key = list(key)
if is_iterator(key):
key = list(key)

if com.is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
key = np.asarray(key, dtype=bool)
return self._get_values(key)
if com.is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
key = np.asarray(key, dtype=bool)
return self._get_values(key)

return self._get_with(key)

Expand Down