Skip to content

Commit 7ef02b4

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

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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)