Skip to content

Commit d649f9e

Browse files
committed
Deprecated fastpath parameter in Categorial constructor
1 parent 4292a27 commit d649f9e

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

pandas/core/categorical.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
287287
# if dtype.categories is None, we are inferring
288288

289289
if fastpath:
290+
warn("`fastpath` parameter is deprecated. Use `dtype` parameter "
291+
"to construct or pass `CategorialDtype` instance instead.",
292+
DeprecationWarning, stacklevel=2)
290293
self._codes = coerce_indexer_dtype(values, categories)
291294
self._dtype = dtype
292295
return
@@ -413,7 +416,7 @@ def copy(self):
413416
return self._constructor(values=self._codes.copy(),
414417
categories=self.categories,
415418
ordered=self.ordered,
416-
fastpath=True)
419+
dtype=self.dtype)
417420

418421
def astype(self, dtype, copy=True):
419422
"""
@@ -545,7 +548,7 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
545548
dtype = CategoricalDtype(cats, ordered=False)
546549
codes = inferred_codes
547550

548-
return cls(codes, dtype=dtype, fastpath=True)
551+
return cls(codes, dtype=dtype)
549552

550553
@classmethod
551554
def from_array(cls, data, **kwargs):
@@ -640,13 +643,15 @@ def _get_labels(self):
640643

641644
labels = property(fget=_get_labels, fset=_set_codes)
642645

643-
def _set_categories(self, categories, fastpath=False):
646+
def _set_categories(self, categories=None, dtype=None):
644647
""" Sets new categories inplace
645648
646649
Parameters
647650
----------
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``.
650655
651656
Examples
652657
--------
@@ -661,13 +666,35 @@ def _set_categories(self, categories, fastpath=False):
661666
Categories (2, object): [a, c]
662667
"""
663668

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+
667692
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)):
671698
raise ValueError("new categories need to have the same number of "
672699
"items than the old categories!")
673700

0 commit comments

Comments
 (0)