Skip to content
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

REF: implement Categorical.encode_with_my_categories #37650

Merged
merged 3 commits into from
Nov 9, 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
30 changes: 23 additions & 7 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1693,9 +1693,8 @@ def _validate_listlike(self, target: ArrayLike) -> np.ndarray:
# Indexing on codes is more efficient if categories are the same,
# so we can apply some optimizations based on the degree of
# dtype-matching.
codes = recode_for_categories(
target.codes, target.categories, self.categories, copy=False
)
cat = self._encode_with_my_categories(target)
codes = cat._codes
else:
codes = self.categories.get_indexer(target)

Expand Down Expand Up @@ -1867,8 +1866,8 @@ def _validate_setitem_value(self, value):
"without identical categories"
)
# is_dtype_equal implies categories_match_up_to_permutation
new_codes = self._validate_listlike(value)
value = Categorical.from_codes(new_codes, dtype=self.dtype)
value = self._encode_with_my_categories(value)
return value._codes

# wrap scalars and hashable-listlikes in list
rvalue = value if not is_hashable(value) else [value]
Expand Down Expand Up @@ -2100,8 +2099,8 @@ def equals(self, other: object) -> bool:
if not isinstance(other, Categorical):
return False
elif self._categories_match_up_to_permutation(other):
other_codes = self._validate_listlike(other)
return np.array_equal(self._codes, other_codes)
other = self._encode_with_my_categories(other)
return np.array_equal(self._codes, other._codes)
return False

@classmethod
Expand All @@ -2112,6 +2111,23 @@ def _concat_same_type(self, to_concat):

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

def _encode_with_my_categories(self, other: "Categorical") -> "Categorical":
"""
Re-encode another categorical using this Categorical's categories.

Notes
-----
This assumes we have already checked
self._categories_match_up_to_permutation(other).
"""
# Indexing on codes is more efficient if categories are the same,
# so we can apply some optimizations based on the degree of
# dtype-matching.
codes = recode_for_categories(
other.codes, other.categories, self.categories, copy=False
)
return self._from_backing_data(codes)

def _categories_match_up_to_permutation(self, other: "Categorical") -> bool:
"""
Returns True if categoricals are the same dtype
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _maybe_unwrap(x):
categories = first.categories
ordered = first.ordered

all_codes = [first._validate_listlike(x) for x in to_union]
all_codes = [first._encode_with_my_categories(x)._codes for x in to_union]
new_codes = np.concatenate(all_codes)

if sort_categories and not ignore_order and ordered:
Expand Down