Skip to content

REF: _unbox_scalar, _unbox_listlike for Categorical #36362

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
Sep 15, 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
29 changes: 17 additions & 12 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def func(self, other):

if is_scalar(other):
if other in self.categories:
i = self.categories.get_loc(other)
i = self._unbox_scalar(other)
ret = op(self._codes, i)

if opname not in {"__eq__", "__ge__", "__gt__"}:
Expand Down Expand Up @@ -1184,8 +1184,7 @@ def _validate_searchsorted_value(self, value):
# 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)
codes = self._unbox_scalar(value)
else:
locs = [self.categories.get_loc(x) for x in value]
codes = np.array(locs, dtype=self.codes.dtype)
Expand All @@ -1212,7 +1211,7 @@ def _validate_fill_value(self, fill_value):
if isna(fill_value):
fill_value = -1
elif fill_value in self.categories:
fill_value = self.categories.get_loc(fill_value)
fill_value = self._unbox_scalar(fill_value)
else:
raise ValueError(
f"'fill_value={fill_value}' is not present "
Expand Down Expand Up @@ -1680,7 +1679,7 @@ def fillna(self, value=None, method=None, limit=None):
if isna(value):
codes[mask] = -1
else:
codes[mask] = self.categories.get_loc(value)
codes[mask] = self._unbox_scalar(value)

else:
raise TypeError(
Expand Down Expand Up @@ -1734,6 +1733,17 @@ def _validate_listlike(self, target: ArrayLike) -> np.ndarray:

return codes

def _unbox_scalar(self, key) -> int:
# searchsorted is very performance sensitive. By converting codes
# to same dtype as self.codes, we get much faster performance.
code = self.categories.get_loc(key)
code = self._codes.dtype.type(code)
return code

def _unbox_listlike(self, value):
unboxed = self.categories.get_indexer(value)
return unboxed.astype(self._ndarray.dtype, copy=False)

# ------------------------------------------------------------------

def take_nd(self, indexer, allow_fill: bool = False, fill_value=None):
Expand Down Expand Up @@ -1925,11 +1935,7 @@ def _validate_setitem_value(self, value):
"category, set the categories first"
)

lindexer = self.categories.get_indexer(rvalue)
if isinstance(lindexer, np.ndarray) and lindexer.dtype.kind == "i":
lindexer = lindexer.astype(self._ndarray.dtype)

return lindexer
return self._unbox_listlike(rvalue)

def _validate_setitem_key(self, key):
if lib.is_integer(key):
Expand Down Expand Up @@ -2155,8 +2161,7 @@ def unique(self):
return cat.set_categories(cat.categories.take(take_codes))

def _values_for_factorize(self):
codes = self.codes.astype("int64")
return codes, -1
return self._ndarray, -1

@classmethod
def _from_factorized(cls, uniques, original):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def copy(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT:
return new_obj

def _values_for_factorize(self):
return self.asi8, iNaT
return self._ndarray, iNaT

@classmethod
def _from_factorized(cls, values, original):
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,8 @@ def _reindex_non_unique(self, target):
# --------------------------------------------------------------------
# Indexing Methods

def _maybe_cast_indexer(self, key):
code = self.categories.get_loc(key)
code = self.codes.dtype.type(code)
return code
def _maybe_cast_indexer(self, key) -> int:
return self._data._unbox_scalar(key)

@Appender(_index_shared_docs["get_indexer"] % _index_doc_kwargs)
def get_indexer(self, target, method=None, limit=None, tolerance=None):
Expand Down