@@ -121,26 +121,6 @@ class CategoricalDtype(ExtensionDtype):
121
121
categories : list or None
122
122
ordered : bool, default False
123
123
124
- Notes
125
- -----
126
- An instance of ``CategoricalDtype`` compares equal with any other
127
- instance of ``CategoricalDtype``, regardless of categories or ordered.
128
- In addition they compare equal to the string ``'category'``.
129
-
130
- To check whether two instances of a ``CategoricalDtype`` exactly,
131
- use the ``is`` operator.
132
-
133
- >>> t1 = CategoricalDtype(['a', 'b'], ordered=True)
134
- >>> t2 = CategoricalDtype(['a', 'c'], ordered=False)
135
- >>> t1 == t2
136
- True
137
- >>> t1 == 'category'
138
- True
139
- >>> t1 is t2
140
- False
141
- >>> t1 is CategoricalDtype(['a', 'b'], ordered=True)
142
- True
143
-
144
124
Examples
145
125
--------
146
126
>>> t = CategoricalDtype(categories=['b', 'a'], ordered=True)
@@ -160,17 +140,16 @@ class CategoricalDtype(ExtensionDtype):
160
140
kind = 'O'
161
141
str = '|O08'
162
142
base = np .dtype ('O' )
163
- _metadata = []
143
+ _metadata = ['categories' , 'ordered' ]
164
144
_cache = weakref .WeakValueDictionary ()
165
145
166
146
def __new__ (cls , categories = None , ordered = False , fastpath = False ):
167
147
from pandas .core .indexes .base import Index
168
148
if categories is not None :
169
149
categories = Index (categories )
170
- # Can just inline _validate_* if needed
150
+ # validation
171
151
cls ._validate_categories (categories , fastpath = fastpath )
172
152
cls ._validate_ordered (ordered )
173
-
174
153
# We have a choice when hashing pandas unordered categoricals
175
154
# We can completely ignore the order, or not. I.e. should
176
155
# [a, b, c] hash the same as [b, a, c], when both are unordered?
@@ -184,7 +163,15 @@ def __new__(cls, categories=None, ordered=False, fastpath=False):
184
163
hashed = cls ._hash_categories (categories , ordered = True )
185
164
else :
186
165
hashed = None
187
- return cls ._get_or_create (categories , ordered , hashed )
166
+
167
+ try :
168
+ return cls ._cache [(hashed , ordered )]
169
+ except KeyError :
170
+ categorical = object .__new__ (cls )
171
+ categorical ._categories = categories
172
+ categorical ._ordered = ordered
173
+ cls ._cache [(hashed , ordered )] = categorical
174
+ return categorical
188
175
189
176
def __hash__ (self ):
190
177
# _hash_categories returns a uint64, so use the negative
@@ -237,18 +224,6 @@ def _hash_categories(categories, ordered=True):
237
224
hashed = np .bitwise_xor .reduce (hashed )
238
225
return hashed
239
226
240
- @classmethod
241
- def _get_or_create (cls , categories , ordered , hashed ):
242
-
243
- try :
244
- return cls ._cache [(hashed , ordered )]
245
- except KeyError :
246
- categorical = object .__new__ (cls )
247
- categorical ._categories = categories
248
- categorical ._ordered = ordered
249
- cls ._cache [(hashed , ordered )] = categorical
250
- return categorical
251
-
252
227
@classmethod
253
228
def construct_from_string (cls , string ):
254
229
""" attempt to construct this type from a string, raise a TypeError if
0 commit comments