Skip to content

REF: remove Categorical._validate_listlike, unbox_listlike #37724

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 15 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
0d7a41b
REF: implement Categorical.encode_with_my_categories
jbrockmendel Nov 5, 2020
48940d4
DOC: Fix typo (#37636)
MicaelJarniac Nov 5, 2020
aa94449
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 5, 2020
bfdad27
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 6, 2020
ca5de4a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 7, 2020
76343be
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 8, 2020
6863e49
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 8, 2020
decb3b4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 8, 2020
ebb5034
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 9, 2020
16de2a0
Use _encode_with_my_categories instead of _validate_listlike
jbrockmendel Nov 9, 2020
cd0f56e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 9, 2020
ea298cd
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 9, 2020
12e3979
REF: Categorical._validate_listlike -> CategoricalIndex._get_codes_fo…
jbrockmendel Nov 9, 2020
502f981
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel Nov 9, 2020
30d4cf8
CLN: remove unbox_listlike
jbrockmendel Nov 9, 2020
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
27 changes: 2 additions & 25 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,37 +1680,13 @@ def _box_func(self, i: int):
return np.NaN
return self.categories[i]

def _validate_listlike(self, target: ArrayLike) -> np.ndarray:
"""
Extract integer codes we can use for comparison.

Notes
-----
If a value in target is not present, it gets coded as -1.
"""

if isinstance(target, Categorical):
# Indexing on codes is more efficient if categories are the same,
# so we can apply some optimizations based on the degree of
# dtype-matching.
cat = self._encode_with_my_categories(target)
codes = cat._codes
else:
codes = self.categories.get_indexer(target)

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 @@ -1884,7 +1860,8 @@ def _validate_setitem_value(self, value):
"category, set the categories first"
)

return self._unbox_listlike(rvalue)
codes = self.categories.get_indexer(rvalue)
return codes.astype(self._ndarray.dtype, copy=False)

def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
"""
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/groupby/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def recode_for_groupby(
"""
# we only care about observed values
if observed:
# In cases with c.ordered, this is equivalent to
# return c.remove_unused_categories(), c

unique_codes = unique1d(c.codes)

take_codes = unique_codes[unique_codes != -1]
Expand Down
29 changes: 26 additions & 3 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pandas._libs import index as libindex
from pandas._libs.hashtable import duplicated_int64
from pandas._libs.lib import no_default
from pandas._typing import Label
from pandas._typing import ArrayLike, Label
from pandas.util._decorators import Appender, cache_readonly, doc

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -542,18 +542,41 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
"method='nearest' not implemented yet for CategoricalIndex"
)

codes = self._values._validate_listlike(target._values)
# Note: we use engine.get_indexer_non_unique below because, even if
# `target` is unique, any non-category entries in it will be encoded
# as -1 by _get_codes_for_get_indexer, so `codes` may not be unique.
codes = self._get_codes_for_get_indexer(target._values)
indexer, _ = self._engine.get_indexer_non_unique(codes)
return ensure_platform_int(indexer)

@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
def get_indexer_non_unique(self, target):
target = ibase.ensure_index(target)

codes = self._values._validate_listlike(target._values)
codes = self._get_codes_for_get_indexer(target._values)
indexer, missing = self._engine.get_indexer_non_unique(codes)
return ensure_platform_int(indexer), missing

def _get_codes_for_get_indexer(self, target: ArrayLike) -> np.ndarray:
"""
Extract integer codes we can use for comparison.

Notes
-----
If a value in target is not present, it gets coded as -1.
"""

if isinstance(target, Categorical):
# Indexing on codes is more efficient if categories are the same,
# so we can apply some optimizations based on the degree of
# dtype-matching.
cat = self._data._encode_with_my_categories(target)
codes = cat._codes
else:
codes = self.categories.get_indexer(target)

return codes

@doc(Index._convert_list_indexer)
def _convert_list_indexer(self, keyarr):
# Return our indexer or raise if all of the values are not included in
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,21 +1951,23 @@ def _factorize_keys(
rk, _ = rk._values_for_factorize()

elif (
is_categorical_dtype(lk) and is_categorical_dtype(rk) and is_dtype_equal(lk, rk)
is_categorical_dtype(lk.dtype)
and is_categorical_dtype(rk.dtype)
and is_dtype_equal(lk.dtype, rk.dtype)
):
assert isinstance(lk, Categorical)
assert isinstance(rk, Categorical)
# Cast rk to encoding so we can compare codes with lk
rk = lk._validate_listlike(rk)
rk = lk._encode_with_my_categories(rk)

lk = ensure_int64(lk.codes)
rk = ensure_int64(rk)
rk = ensure_int64(rk.codes)

elif is_extension_array_dtype(lk.dtype) and is_dtype_equal(lk.dtype, rk.dtype):
lk, _ = lk._values_for_factorize()
rk, _ = rk._values_for_factorize()

if is_integer_dtype(lk) and is_integer_dtype(rk):
if is_integer_dtype(lk.dtype) and is_integer_dtype(rk.dtype):
# GH#23917 TODO: needs tests for case where lk is integer-dtype
# and rk is datetime-dtype
klass = libhashtable.Int64Factorizer
Expand Down