@@ -284,6 +284,9 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
284
284
# if dtype.categories is None, we are inferring
285
285
286
286
if fastpath :
287
+ warn ("`fastpath` parameter is deprecated. Use `dtype` parameter "
288
+ "to construct or pass `CategorialDtype` instance instead." ,
289
+ DeprecationWarning , stacklevel = 2 )
287
290
self ._codes = coerce_indexer_dtype (values , categories )
288
291
self ._dtype = dtype
289
292
return
@@ -431,7 +434,7 @@ def copy(self):
431
434
return self ._constructor (values = self ._codes .copy (),
432
435
categories = self .categories ,
433
436
ordered = self .ordered ,
434
- fastpath = True )
437
+ dtype = self . dtype )
435
438
436
439
def astype (self , dtype , copy = True ):
437
440
"""
@@ -563,7 +566,7 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
563
566
dtype = CategoricalDtype (cats , ordered = False )
564
567
codes = inferred_codes
565
568
566
- return cls (codes , dtype = dtype , fastpath = True )
569
+ return cls (codes , dtype = dtype )
567
570
568
571
@classmethod
569
572
def from_array (cls , data , ** kwargs ):
@@ -658,13 +661,15 @@ def _get_labels(self):
658
661
659
662
labels = property (fget = _get_labels , fset = _set_codes )
660
663
661
- def _set_categories (self , categories , fastpath = False ):
664
+ def _set_categories (self , categories = None , dtype = None ):
662
665
""" Sets new categories inplace
663
666
664
667
Parameters
665
668
----------
666
- fastpath : boolean (default: False)
667
- Don't perform validation of the categories for uniqueness or nulls
669
+ categories : Index-like (unique)
670
+ Unique new categories
671
+ dtype : CategorialDtype or string (Default: None)
672
+ An instance of ``CategorialDtype``.
668
673
669
674
Examples
670
675
--------
@@ -679,13 +684,35 @@ def _set_categories(self, categories, fastpath=False):
679
684
Categories (2, object): [a, c]
680
685
"""
681
686
682
- if fastpath :
683
- new_dtype = CategoricalDtype ._from_fastpath (categories ,
684
- self .ordered )
687
+ if dtype is None and categories is None :
688
+ raise ValueError ("categories and dtype both cannot be None" )
689
+
690
+ if dtype is not None :
691
+ if isinstance (dtype , compat .string_types ):
692
+ if dtype == 'category' :
693
+ new_dtype = CategorialDtype (categories , self .ordered )
694
+
695
+ else :
696
+ msg = "Unknown `dtype` {dtype}"
697
+ raise ValueError (msg .format (dtype = dtype ))
698
+
699
+ elif categories is not None :
700
+ raise ValueError ("Cannot specify both categories and dtype" )
701
+
702
+
703
+ else :
704
+ if not (isinstance (dtype , CategorialDtype )):
705
+ raise ValueError ("dtype must be an instance of "
706
+ "CategorialDtype" )
707
+ else :
708
+ new_dtype = dtype
709
+
685
710
else :
686
- new_dtype = CategoricalDtype (categories , ordered = self .ordered )
687
- if (not fastpath and self .dtype .categories is not None and
688
- len (new_dtype .categories ) != len (self .dtype .categories )):
711
+ new_dtype = CategorialDtype (categories , self .ordered )
712
+
713
+
714
+ if (self .dtype .categories is not None and
715
+ len (new_dtype .categories ) != len (self .dtype .categories )):
689
716
raise ValueError ("new categories need to have the same number of "
690
717
"items than the old categories!" )
691
718
0 commit comments