@@ -287,6 +287,9 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
287
287
# if dtype.categories is None, we are inferring
288
288
289
289
if fastpath :
290
+ warn ("`fastpath` parameter is deprecated. Use `dtype` parameter "
291
+ "to construct or pass `CategorialDtype` instance instead." ,
292
+ DeprecationWarning , stacklevel = 2 )
290
293
self ._codes = coerce_indexer_dtype (values , categories )
291
294
self ._dtype = dtype
292
295
return
@@ -413,7 +416,7 @@ def copy(self):
413
416
return self ._constructor (values = self ._codes .copy (),
414
417
categories = self .categories ,
415
418
ordered = self .ordered ,
416
- fastpath = True )
419
+ dtype = self . dtype )
417
420
418
421
def astype (self , dtype , copy = True ):
419
422
"""
@@ -545,7 +548,7 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
545
548
dtype = CategoricalDtype (cats , ordered = False )
546
549
codes = inferred_codes
547
550
548
- return cls (codes , dtype = dtype , fastpath = True )
551
+ return cls (codes , dtype = dtype )
549
552
550
553
@classmethod
551
554
def from_array (cls , data , ** kwargs ):
@@ -640,13 +643,15 @@ def _get_labels(self):
640
643
641
644
labels = property (fget = _get_labels , fset = _set_codes )
642
645
643
- def _set_categories (self , categories , fastpath = False ):
646
+ def _set_categories (self , categories = None , dtype = None ):
644
647
""" Sets new categories inplace
645
648
646
649
Parameters
647
650
----------
648
- fastpath : boolean (default: False)
649
- Don't perform validation of the categories for uniqueness or nulls
651
+ categories : Index-like (unique)
652
+ Unique new categories
653
+ dtype : CategorialDtype or string (Default: None)
654
+ An instance of ``CategorialDtype``.
650
655
651
656
Examples
652
657
--------
@@ -661,13 +666,35 @@ def _set_categories(self, categories, fastpath=False):
661
666
Categories (2, object): [a, c]
662
667
"""
663
668
664
- if fastpath :
665
- new_dtype = CategoricalDtype ._from_fastpath (categories ,
666
- self .ordered )
669
+ if dtype is None and categories is None :
670
+ raise ValueError ("categories and dtype both cannot be None" )
671
+
672
+ if dtype is not None :
673
+ if isinstance (dtype , compat .string_types ):
674
+ if dtype == 'category' :
675
+ new_dtype = CategorialDtype (categories , self .ordered )
676
+
677
+ else :
678
+ msg = "Unknown `dtype` {dtype}"
679
+ raise ValueError (msg .format (dtype = dtype ))
680
+
681
+ elif categories is not None :
682
+ raise ValueError ("Cannot specify both categories and dtype" )
683
+
684
+
685
+ else :
686
+ if not (isinstance (dtype , CategorialDtype )):
687
+ raise ValueError ("dtype must be an instance of "
688
+ "CategorialDtype" )
689
+ else :
690
+ new_dtype = dtype
691
+
667
692
else :
668
- new_dtype = CategoricalDtype (categories , ordered = self .ordered )
669
- if (not fastpath and self .dtype .categories is not None and
670
- len (new_dtype .categories ) != len (self .dtype .categories )):
693
+ new_dtype = CategorialDtype (categories , self .ordered )
694
+
695
+
696
+ if (self .dtype .categories is not None and
697
+ len (new_dtype .categories ) != len (self .dtype .categories )):
671
698
raise ValueError ("new categories need to have the same number of "
672
699
"items than the old categories!" )
673
700
0 commit comments