Skip to content

Commit f76806c

Browse files
committed
Deprecated fastpath parameter in Categorial constructor
1 parent 00f23ca commit f76806c

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

pandas/core/categorical.py

+38-11
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
284284
# if dtype.categories is None, we are inferring
285285

286286
if fastpath:
287+
warn("`fastpath` parameter is deprecated. Use `dtype` parameter "
288+
"to construct or pass `CategorialDtype` instance instead.",
289+
DeprecationWarning, stacklevel=2)
287290
self._codes = coerce_indexer_dtype(values, categories)
288291
self._dtype = dtype
289292
return
@@ -431,7 +434,7 @@ def copy(self):
431434
return self._constructor(values=self._codes.copy(),
432435
categories=self.categories,
433436
ordered=self.ordered,
434-
fastpath=True)
437+
dtype=self.dtype)
435438

436439
def astype(self, dtype, copy=True):
437440
"""
@@ -563,7 +566,7 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
563566
dtype = CategoricalDtype(cats, ordered=False)
564567
codes = inferred_codes
565568

566-
return cls(codes, dtype=dtype, fastpath=True)
569+
return cls(codes, dtype=dtype)
567570

568571
@classmethod
569572
def from_array(cls, data, **kwargs):
@@ -658,13 +661,15 @@ def _get_labels(self):
658661

659662
labels = property(fget=_get_labels, fset=_set_codes)
660663

661-
def _set_categories(self, categories, fastpath=False):
664+
def _set_categories(self, categories=None, dtype=None):
662665
""" Sets new categories inplace
663666
664667
Parameters
665668
----------
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``.
668673
669674
Examples
670675
--------
@@ -679,13 +684,35 @@ def _set_categories(self, categories, fastpath=False):
679684
Categories (2, object): [a, c]
680685
"""
681686

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+
685710
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)):
689716
raise ValueError("new categories need to have the same number of "
690717
"items than the old categories!")
691718

0 commit comments

Comments
 (0)