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

PERF: avoid is_bool_indexer check where possible #31399

Merged
merged 3 commits into from
Feb 4, 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
49 changes: 27 additions & 22 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,31 +851,36 @@ def __getitem__(self, key):
if key is Ellipsis:
return self

try:
result = self.index.get_value(self, key)
key_is_scalar = is_scalar(key)

return result
except InvalidIndexError:
pass
except (KeyError, ValueError):
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
# kludge
pass
elif com.is_bool_indexer(key):
if key_is_scalar or isinstance(self.index, MultiIndex):
# Otherwise index.get_value will raise InvalidIndexError
try:
result = self.index.get_value(self, key)
jreback marked this conversation as resolved.
Show resolved Hide resolved

return result
except InvalidIndexError:
pass
else:
except (KeyError, ValueError):
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
# kludge
Copy link
Member

Choose a reason for hiding this comment

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

Such a comment is not really helpful if you don't explain the kludge ..

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed, but this comment is present in the status quo

pass
else:

# we can try to coerce the indexer (or this will raise)
new_key = self.index._convert_scalar_indexer(key, kind="getitem")
if type(new_key) != type(key):
return self.__getitem__(new_key)
raise
# we can try to coerce the indexer (or this will raise)
new_key = self.index._convert_scalar_indexer(key, kind="getitem")
if type(new_key) != type(key):
return self.__getitem__(new_key)
raise

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

if com.is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
if com.is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
return self._get_values(key)

return self._get_with(key)

Expand Down Expand Up @@ -908,6 +913,8 @@ def _get_with(self, key):
else:
key_type = lib.infer_dtype(key, skipna=False)

# Note: The key_type == "boolean" case should be caught by the
# com.is_bool_indexer check in __getitem__
if key_type == "integer":
if self.index.is_integer() or self.index.is_floating():
return self.loc[key]
Expand All @@ -916,8 +923,6 @@ def _get_with(self, key):
return self.iloc[indexer]
else:
return self._get_values(key)
elif key_type == "boolean":
return self._get_values(key)

if isinstance(key, (list, tuple)):
# TODO: de-dup with tuple case handled above?
Expand Down