Skip to content

REF: use _validate_foo pattern in Categorical #36181

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 2 commits into from
Sep 7, 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
31 changes: 22 additions & 9 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,26 @@ def map(self, mapper):
__le__ = _cat_compare_op(operator.le)
__ge__ = _cat_compare_op(operator.ge)

def _validate_insert_value(self, value) -> int:
code = self.categories.get_indexer([value])
if (code == -1) and not (is_scalar(value) and isna(value)):
raise TypeError(
"cannot insert an item into a CategoricalIndex "
"that is not already an existing category"
)
return code[0]

def _validate_searchsorted_value(self, value):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you type 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.

sure, there are a few of these and im planning to do a typing pass at the end

Copy link
Contributor

Choose a reason for hiding this comment

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

kk

# searchsorted is very performance sensitive. By converting codes
# to same dtype as self.codes, we get much faster performance.
if is_scalar(value):
codes = self.categories.get_loc(value)
codes = self.codes.dtype.type(codes)
else:
locs = [self.categories.get_loc(x) for x in value]
codes = np.array(locs, dtype=self.codes.dtype)
return codes

def _validate_fill_value(self, fill_value):
"""
Convert a user-facing fill_value to a representation to use with our
Expand Down Expand Up @@ -1299,15 +1319,8 @@ def memory_usage(self, deep=False):

@doc(_shared_docs["searchsorted"], klass="Categorical")
def searchsorted(self, value, side="left", sorter=None):
# searchsorted is very performance sensitive. By converting codes
# to same dtype as self.codes, we get much faster performance.
if is_scalar(value):
codes = self.categories.get_loc(value)
codes = self.codes.dtype.type(codes)
else:
locs = [self.categories.get_loc(x) for x in value]
codes = np.array(locs, dtype=self.codes.dtype)
return self.codes.searchsorted(codes, side=side, sorter=sorter)
value = self._validate_searchsorted_value(value)
return self.codes.searchsorted(value, side=side, sorter=sorter)

def isna(self):
"""
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
pandas_dtype,
)
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, notna

from pandas.core import accessor
from pandas.core.algorithms import take_1d
Expand Down Expand Up @@ -734,15 +734,10 @@ def insert(self, loc: int, item):
ValueError if the item is not in the categories

"""
code = self.categories.get_indexer([item])
if (code == -1) and not (is_scalar(item) and isna(item)):
raise TypeError(
"cannot insert an item into a CategoricalIndex "
"that is not already an existing category"
)
code = self._data._validate_insert_value(item)

codes = self.codes
codes = np.concatenate((codes[:loc], code, codes[loc:]))
codes = np.concatenate((codes[:loc], [code], codes[loc:]))
return self._create_from_codes(codes)

def _concat(self, to_concat, name):
Expand Down