Skip to content

CLN: simplify CategoricalIndex._simple_new #32204

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
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 7 additions & 32 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
is_scalar,
)
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.generic import ABCCategorical, ABCSeries
from pandas.core.dtypes.missing import isna

from pandas.core import accessor
Expand Down Expand Up @@ -193,7 +192,9 @@ def __new__(
raise cls._scalar_data_error(data)
data = []

data = cls._create_categorical(data, dtype=dtype)
assert isinstance(dtype, CategoricalDtype), dtype
if not isinstance(data, Categorical) or data.dtype != dtype:
data = Categorical(data, dtype=dtype)

data = data.copy() if copy else data

Expand Down Expand Up @@ -223,37 +224,10 @@ def _create_from_codes(self, codes, dtype=None, name=None):
return CategoricalIndex(cat, name=name)

@classmethod
def _create_categorical(cls, data, dtype=None):
"""
*this is an internal non-public method*

create the correct categorical from data and the properties

Parameters
----------
data : data for new Categorical
dtype : CategoricalDtype, defaults to existing

Returns
-------
Categorical
"""
if isinstance(data, (cls, ABCSeries)) and is_categorical_dtype(data):
data = data.values

if not isinstance(data, ABCCategorical):
return Categorical(data, dtype=dtype)

if isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
# we want to silently ignore dtype='category'
data = data._set_dtype(dtype)
return data

@classmethod
def _simple_new(cls, values, name=None, dtype=None):
def _simple_new(cls, values: Categorical, name=None):
Copy link
Member

Choose a reason for hiding this comment

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

this would fix-up mypy

Suggested change
def _simple_new(cls, values: Categorical, name=None):
def _simple_new(cls, values: Categorical, name=None, dtype=None):

and then assert dtype is None or CategoricalDtype?

Copy link
Member

Choose a reason for hiding this comment

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

maybe a docstring/comment to say dtype is for compatibility with Index._simple_new and not used, but if specified and not CategoricalDtype will raise.

assert isinstance(values, Categorical), type(values)
result = object.__new__(cls)

values = cls._create_categorical(values, dtype=dtype)
result._data = values
result.name = name

Expand Down Expand Up @@ -295,7 +269,8 @@ def _is_dtype_compat(self, other) -> bool:
values = other
if not is_list_like(values):
values = [values]
other = CategoricalIndex(self._create_categorical(other, dtype=self.dtype))
cat = Categorical(other, dtype=self.dtype)
other = CategoricalIndex(cat)
if not other.isin(values).all():
raise TypeError(
"cannot append a non-category item to a CategoricalIndex"
Expand Down
9 changes: 0 additions & 9 deletions pandas/tests/indexes/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,3 @@ def test_construction_with_categorical_dtype(self):

with pytest.raises(ValueError, match=msg):
Index(data, ordered=ordered, dtype=dtype)

def test_create_categorical(self):
Copy link
Member

Choose a reason for hiding this comment

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

Does this no longer work? I think should keep the test no?

Copy link
Member Author

Choose a reason for hiding this comment

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

the method it is testing is removed

# GH#17513 The public CI constructor doesn't hit this code path with
# instances of CategoricalIndex, but we still want to test the code
ci = CategoricalIndex(["a", "b", "c"])
# First ci is self, second ci is data.
result = CategoricalIndex._create_categorical(ci, ci)
expected = Categorical(["a", "b", "c"])
tm.assert_categorical_equal(result, expected)