Skip to content

Commit 6111f64

Browse files
topper-123jreback
authored andcommitted
claenup some CategoricalIndex internals (pandas-dev#24343)
1 parent 2c8b207 commit 6111f64

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

pandas/core/indexes/category.py

+15-40
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
133133

134134
return cls._simple_new(data, name=name)
135135

136-
def _create_from_codes(self, codes, categories=None, ordered=None,
137-
name=None):
136+
def _create_from_codes(self, codes, dtype=None, name=None):
138137
"""
139138
*this is an internal non-public method*
140139
@@ -143,23 +142,20 @@ def _create_from_codes(self, codes, categories=None, ordered=None,
143142
Parameters
144143
----------
145144
codes : new codes
146-
categories : optional categories, defaults to existing
147-
ordered : optional ordered attribute, defaults to existing
145+
dtype: CategoricalDtype, defaults to existing
148146
name : optional name attribute, defaults to existing
149147
150148
Returns
151149
-------
152150
CategoricalIndex
153151
"""
154152

155-
if categories is None:
156-
categories = self.categories
157-
if ordered is None:
158-
ordered = self.ordered
153+
if dtype is None:
154+
dtype = self.dtype
159155
if name is None:
160156
name = self.name
161-
cat = Categorical.from_codes(codes, categories=categories,
162-
ordered=ordered)
157+
cat = Categorical.from_codes(codes, categories=dtype.categories,
158+
ordered=dtype.ordered)
163159
return CategoricalIndex(cat, name=name)
164160

165161
@classmethod
@@ -201,12 +197,10 @@ def _create_categorical(cls, data, categories=None, ordered=None,
201197
return data
202198

203199
@classmethod
204-
def _simple_new(cls, values, name=None, categories=None, ordered=None,
205-
dtype=None, **kwargs):
200+
def _simple_new(cls, values, name=None, dtype=None, **kwargs):
206201
result = object.__new__(cls)
207202

208-
values = cls._create_categorical(values, categories, ordered,
209-
dtype=dtype)
203+
values = cls._create_categorical(values, dtype=dtype)
210204
result._data = values
211205
result.name = name
212206
for k, v in compat.iteritems(kwargs):
@@ -218,29 +212,11 @@ def _simple_new(cls, values, name=None, categories=None, ordered=None,
218212
# --------------------------------------------------------------------
219213

220214
@Appender(_index_shared_docs['_shallow_copy'])
221-
def _shallow_copy(self, values=None, categories=None, ordered=None,
222-
dtype=None, **kwargs):
223-
# categories and ordered can't be part of attributes,
224-
# as these are properties
225-
# we want to reuse self.dtype if possible, i.e. neither are
226-
# overridden.
227-
if dtype is not None and (categories is not None or
228-
ordered is not None):
229-
raise TypeError("Cannot specify both `dtype` and `categories` "
230-
"or `ordered`")
231-
232-
if categories is None and ordered is None:
233-
dtype = self.dtype if dtype is None else dtype
234-
return super(CategoricalIndex, self)._shallow_copy(
235-
values=values, dtype=dtype, **kwargs)
236-
if categories is None:
237-
categories = self.categories
238-
if ordered is None:
239-
ordered = self.ordered
240-
215+
def _shallow_copy(self, values=None, dtype=None, **kwargs):
216+
if dtype is None:
217+
dtype = self.dtype
241218
return super(CategoricalIndex, self)._shallow_copy(
242-
values=values, categories=categories,
243-
ordered=ordered, **kwargs)
219+
values=values, dtype=dtype, **kwargs)
244220

245221
def _is_dtype_compat(self, other):
246222
"""
@@ -425,10 +401,9 @@ def unique(self, level=None):
425401
if level is not None:
426402
self._validate_index_level(level)
427403
result = self.values.unique()
428-
# CategoricalIndex._shallow_copy keeps original categories
429-
# and ordered if not otherwise specified
430-
return self._shallow_copy(result, categories=result.categories,
431-
ordered=result.ordered)
404+
# CategoricalIndex._shallow_copy keeps original dtype
405+
# if not otherwise specified
406+
return self._shallow_copy(result, dtype=result.dtype)
432407

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

0 commit comments

Comments
 (0)