8000 PERF: __contains__ method for Categorical by fjetter · Pull Request #21022 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

PERF: __contains__ method for Categorical #21022

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

Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
PERF: __contains__ of Categorical improved
  • Loading branch information
fjetter committed Jun 9, 2018
commit c5387a71482bfe7ccc1e730d24765ea812f617c7
12 changes: 8 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,10 +1849,14 @@ def __iter__(self):

def __contains__(self, key):
"""Returns True if `key` is in this Categorical."""
if key in self.categories:
return self.categories.get_loc(key) in self.codes
elif isna(key) and self.isna().any():
return True
hash(key)
if isna(key):
return self.isna().any()
elif self.categories._defer_to_indexing: # e.g. Interval values
loc = self.categories.get_loc(key)
return np.isin(self.codes, loc).any()
elif key in self.categories:
return self.categories.get_loc(key) in self._codes
else:
return False

Expand Down
0