REF: use another ctor for fastpath · pandas-dev/pandas@91752fe (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -969,8 +969,8 @@ def remove_unused_categories(self, inplace=False):
969 969 idx, inv = idx[1:], inv - 1
970 970
971 971 new_categories = cat.dtype.categories.take(idx)
972 -new_dtype = CategoricalDtype(new_categories, ordered=self.ordered,
973 -fastpath=True)
972 +new_dtype = CategoricalDtype._from_fastpath(new_categories,
973 + ordered=self.ordered)
974 974 cat._dtype = new_dtype
975 975 cat._codes = coerce_indexer_dtype(inv, new_dtype.categories)
976 976
Original file line number Diff line number Diff line change
@@ -152,18 +152,25 @@ class CategoricalDtype(ExtensionDtype):
152 152 _metadata = ['categories', 'ordered']
153 153 _cache = {}
154 154
155 -def __new__(cls, categories=None, ordered=False, fastpath=False):
155 +def __init__(self, categories=None, ordered=False):
156 +self._finalize(categories, ordered, fastpath=False)
157 +
158 +@classmethod
159 +def _from_fastpath(cls, categories=None, ordered=False):
160 +self = cls.__new__(cls)
161 +self._finalize(categories, ordered, fastpath=True)
162 +return self
163 +
164 +def _finalize(self, categories, ordered, fastpath=False):
156 165 from pandas.core.indexes.base import Index
157 166
158 167 if categories is not None:
159 168 categories = Index(categories, tupleize_cols=False)
160 169 # validation
161 -cls._validate_categories(categories, fastpath=fastpath)
162 -cls._validate_ordered(ordered)
163 -categorical = object.__new__(cls)
164 -categorical._categories = categories
165 -categorical._ordered = ordered
166 -return categorical
170 +self._validate_categories(categories)
171 +self._validate_ordered(ordered)
172 +self._categories = categories
173 +self._ordered = ordered
167 174
168 175 def __hash__(self):
169 176 # _hash_categories returns a uint64, so use the negative