Skip to content

CLN: simplify CategoricalIndex internals #24343

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
Dec 18, 2018
Merged
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
55 changes: 15 additions & 40 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def __new__(cls, data=None, categories=None, ordered=None, dtype=None,

return cls._simple_new(data, name=name)

def _create_from_codes(self, codes, categories=None, ordered=None,
name=None):
def _create_from_codes(self, codes, dtype=None, name=None):
"""
*this is an internal non-public method*

Expand All @@ -143,23 +142,20 @@ def _create_from_codes(self, codes, categories=None, ordered=None,
Parameters
----------
codes : new codes
categories : optional categories, defaults to existing
ordered : optional ordered attribute, defaults to existing
dtype: CategoricalDtype, defaults to existing
name : optional name attribute, defaults to existing

Returns
-------
CategoricalIndex
"""

if categories is None:
categories = self.categories
if ordered is None:
ordered = self.ordered
if dtype is None:
dtype = self.dtype
if name is None:
name = self.name
cat = Categorical.from_codes(codes, categories=categories,
ordered=ordered)
cat = Categorical.from_codes(codes, categories=dtype.categories,
ordered=dtype.ordered)
return CategoricalIndex(cat, name=name)

@classmethod
Expand Down Expand Up @@ -201,12 +197,10 @@ def _create_categorical(cls, data, categories=None, ordered=None,
return data

@classmethod
def _simple_new(cls, values, name=None, categories=None, ordered=None,
dtype=None, **kwargs):
def _simple_new(cls, values, name=None, dtype=None, **kwargs):
result = object.__new__(cls)

values = cls._create_categorical(values, categories, ordered,
dtype=dtype)
values = cls._create_categorical(values, dtype=dtype)
result._data = values
result.name = name
for k, v in compat.iteritems(kwargs):
Expand All @@ -218,29 +212,11 @@ def _simple_new(cls, values, name=None, categories=None, ordered=None,
# --------------------------------------------------------------------

@Appender(_index_shared_docs['_shallow_copy'])
def _shallow_copy(self, values=None, categories=None, ordered=None,
dtype=None, **kwargs):
# categories and ordered can't be part of attributes,
# as these are properties
# we want to reuse self.dtype if possible, i.e. neither are
# overridden.
if dtype is not None and (categories is not None or
ordered is not None):
raise TypeError("Cannot specify both `dtype` and `categories` "
"or `ordered`")

if categories is None and ordered is None:
dtype = self.dtype if dtype is None else dtype
return super(CategoricalIndex, self)._shallow_copy(
values=values, dtype=dtype, **kwargs)
if categories is None:
categories = self.categories
if ordered is None:
ordered = self.ordered

def _shallow_copy(self, values=None, dtype=None, **kwargs):
if dtype is None:
dtype = self.dtype
return super(CategoricalIndex, self)._shallow_copy(
values=values, categories=categories,
ordered=ordered, **kwargs)
values=values, dtype=dtype, **kwargs)

def _is_dtype_compat(self, other):
"""
Expand Down Expand Up @@ -425,10 +401,9 @@ def unique(self, level=None):
if level is not None:
self._validate_index_level(level)
result = self.values.unique()
# CategoricalIndex._shallow_copy keeps original categories
# and ordered if not otherwise specified
return self._shallow_copy(result, categories=result.categories,
ordered=result.ordered)
# CategoricalIndex._shallow_copy keeps original dtype
# if not otherwise specified
return self._shallow_copy(result, dtype=result.dtype)

@Appender(Index.duplicated.__doc__)
def duplicated(self, keep='first'):
Expand Down