@@ -293,28 +293,12 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
293
293
294
294
# sanitize input
295
295
if is_categorical_dtype (values ):
296
+ if dtype .categories is None :
297
+ dtype = CategoricalDtype (values .categories , dtype .ordered )
296
298
297
- # we are either a Series or a CategoricalIndex
298
- if isinstance (values , (ABCSeries , ABCCategoricalIndex )):
299
- values = values ._values
300
-
301
- if ordered is None :
302
- ordered = values .ordered
303
- if categories is None :
304
- categories = values .categories
305
- values = values .get_values ()
306
-
307
- elif isinstance (values , (ABCIndexClass , ABCSeries )):
308
- # we'll do inference later
309
- pass
310
-
311
- else :
312
-
313
- # on numpy < 1.6 datetimelike get inferred to all i8 by
314
- # _sanitize_array which is fine, but since factorize does this
315
- # correctly no need here this is an issue because _sanitize_array
316
- # also coerces np.nan to a string under certain versions of numpy
317
- # as well
299
+ elif not isinstance (values , (ABCIndexClass , ABCSeries )):
300
+ # _sanitize_array coerces np.nan to a string under certain versions
301
+ # of numpy
318
302
values = maybe_infer_to_datetimelike (values , convert_dates = True )
319
303
if not isinstance (values , np .ndarray ):
320
304
values = _convert_to_list_like (values )
@@ -334,7 +318,7 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
334
318
codes , categories = factorize (values , sort = True )
335
319
except TypeError :
336
320
codes , categories = factorize (values , sort = False )
337
- if ordered :
321
+ if dtype . ordered :
338
322
# raise, as we don't have a sortable data structure and so
339
323
# the user should give us one by specifying categories
340
324
raise TypeError ("'values' is not ordered, please "
@@ -346,9 +330,14 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
346
330
raise NotImplementedError ("> 1 ndim Categorical are not "
347
331
"supported at this time" )
348
332
349
- if dtype .categories is None :
350
- # we're inferring from values
351
- dtype = CategoricalDtype (categories , ordered )
333
+ # we're inferring from values
334
+ dtype = CategoricalDtype (categories , dtype .ordered )
335
+
336
+ elif is_categorical_dtype (values ):
337
+ old_codes = (values .cat .codes if isinstance (values , ABCSeries )
338
+ else values .codes )
339
+ codes = _recode_for_categories (old_codes , values .dtype .categories ,
340
+ dtype .categories )
352
341
353
342
else :
354
343
codes = _get_codes_for_values (values , dtype .categories )
0 commit comments