Skip to content

Commit 91752fe

Browse files
committed
REF: use another ctor for fastpath
Also use __init__ instead of __new__ for simplicity.
1 parent 7314570 commit 91752fe

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

pandas/core/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,8 @@ def remove_unused_categories(self, inplace=False):
969969
idx, inv = idx[1:], inv - 1
970970

971971
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)
974974
cat._dtype = new_dtype
975975
cat._codes = coerce_indexer_dtype(inv, new_dtype.categories)
976976

pandas/core/dtypes/dtypes.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,25 @@ class CategoricalDtype(ExtensionDtype):
152152
_metadata = ['categories', 'ordered']
153153
_cache = {}
154154

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):
156165
from pandas.core.indexes.base import Index
157166

158167
if categories is not None:
159168
categories = Index(categories, tupleize_cols=False)
160169
# 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
167174

168175
def __hash__(self):
169176
# _hash_categories returns a uint64, so use the negative

0 commit comments

Comments
 (0)