Skip to content

TYP: is_dtype_compat #37340

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 3 commits into from
Oct 23, 2020
Merged
Changes from 2 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
10 changes: 7 additions & 3 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,23 @@ def _shallow_copy(self, values=None, name: Label = no_default):

return super()._shallow_copy(values=values, name=name)

def _is_dtype_compat(self, other) -> bool:
def _is_dtype_compat(self, other) -> Categorical:
"""
*this is an internal non-public method*

provide a comparison between the dtype of self and other (coercing if
needed)

Returns
-------
Categorical

Raises
------
TypeError if the dtypes are not compatible
"""
if is_categorical_dtype(other):
if isinstance(other, CategoricalIndex):
other = other._values
other = extract_array(other)
if not other.is_dtype_equal(self):
raise TypeError(
"categories must match existing categories when appending"
Expand All @@ -263,6 +266,7 @@ def _is_dtype_compat(self, other) -> bool:
raise TypeError(
"cannot append a non-category item to a CategoricalIndex"
)
other = other._values

return other

Expand Down