@@ -202,7 +202,6 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
202
202
"""
203
203
204
204
# TODO: Document public vs. private API
205
- name = "category"
206
205
type : type [CategoricalDtypeType ] = CategoricalDtypeType
207
206
kind : str_type = "O"
208
207
str = "|O08"
@@ -315,12 +314,12 @@ def _from_values_or_dtype(
315
314
if dtype is not None :
316
315
# The dtype argument takes precedence over values.dtype (if any)
317
316
if isinstance (dtype , str ):
318
- if dtype == "category" :
317
+ if dtype . startswith ( "category" ) :
319
318
if ordered is None and cls .is_dtype (values ):
320
319
# GH#49309 preserve orderedness
321
320
ordered = values .dtype .ordered
322
-
323
- dtype = CategoricalDtype (categories , ordered )
321
+ cat_dtype = cls . _get_categories_dtype_from_string ( dtype )
322
+ dtype = CategoricalDtype (categories , ordered , cat_dtype )
324
323
else :
325
324
raise ValueError (f"Unknown dtype { repr (dtype )} " )
326
325
elif categories is not None or ordered is not None :
@@ -371,20 +370,27 @@ def construct_from_string(cls, string: str_type) -> CategoricalDtype:
371
370
# need ordered=None to ensure that operations specifying dtype="category" don't
372
371
# override the ordered value for existing categoricals
373
372
374
- if string == cls . name :
373
+ if string == "category" :
375
374
return cls (ordered = None )
376
375
377
376
msg = f"Cannot construct a '{ cls .__name__ } ' from '{ string } '"
377
+ categories_dtype = cls ._get_categories_dtype_from_string (string )
378
+ if categories_dtype is None :
379
+ raise TypeError (msg )
380
+ try :
381
+ return cls (categories_dtype = categories_dtype )
382
+ except (KeyError , TypeError , ValueError ) as err :
383
+ # keyError is if "categories_dtype" key is not found
384
+ # TypeError if we pass a nonsense;
385
+ raise TypeError (msg ) from err
386
+
387
+ @classmethod
388
+ def _get_categories_dtype_from_string (cls , string : str_type ) -> str_type | None :
378
389
match = cls ._match .match (string )
379
- if match :
380
- d = match .groupdict ()
381
- try :
382
- return cls (categories_dtype = d ["categories_dtype" ])
383
- except (KeyError , TypeError , ValueError ) as err :
384
- # keyError is if "categories_dtype" key is not found
385
- # TypeError if we pass a nonsense;
386
- raise TypeError (msg ) from err
387
- raise TypeError (msg )
390
+ if match is None :
391
+ return None
392
+ d = match .groupdict ()
393
+ return d .get ("categories_dtype" )
388
394
389
395
@property
390
396
def categories_dtype (self ) -> Dtype :
@@ -435,7 +441,7 @@ def __eq__(self, other: Any) -> bool:
435
441
6) Any other comparison returns False
436
442
"""
437
443
if isinstance (other , str ):
438
- return other == self .name
444
+ return other == self .name or other == "category"
439
445
elif other is self :
440
446
return True
441
447
elif not (hasattr (other , "ordered" ) and hasattr (other , "categories" )):
@@ -497,6 +503,15 @@ def __repr__(self) -> str_type:
497
503
f"categories_dtype={ self .categories_dtype } )"
498
504
)
499
505
506
+ @property
507
+ def name (self ) -> str_type :
508
+ if self .categories is not None :
509
+ return f"category[{ self .categories .dtype } ]"
510
+ elif self .categories_dtype is not None :
511
+ return f"category[{ self .categories_dtype } ]"
512
+ else :
513
+ return "category"
514
+
500
515
@cache_readonly
501
516
def _hash_categories (self ) -> int :
502
517
from pandas .core .util .hashing import (
0 commit comments