-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
PERF: avoid is_bool_indexer check where possible #31399
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
jorisvandenbossche
merged 3 commits into
pandas-dev:master
from
jbrockmendel:perf-getitem3
Feb 4, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
return result | ||
except InvalidIndexError: | ||
pass | ||
else: | ||
except (KeyError, ValueError): | ||
if isinstance(key, tuple) and isinstance(self.index, MultiIndex): | ||
# kludge | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 .. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
@@ -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] | ||
|
@@ -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? | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.