@@ -569,14 +569,40 @@ def map(self, mapper):
569
569
mapped = self ._values .map (mapper )
570
570
return Index (mapped , name = self .name )
571
571
572
+ def _permutation_check (self , to_concat ) -> bool :
573
+ # GH40378 - we have to check if they are permutations.
574
+ # If they are, we have to create a fix, in order to
575
+ # prevent the error discussed in the named issue
576
+ permutations = True
577
+ category1 = CategoricalIndex (
578
+ [],
579
+ categories = to_concat [0 ]._is_dtype_compat (to_concat [0 ]).categories ,
580
+ ordered = False ,
581
+ )
582
+ for i in to_concat [1 :]:
583
+ category2 = CategoricalIndex (
584
+ [], categories = i ._is_dtype_compat (i ).categories , ordered = False
585
+ )
586
+ if not category1 .equals (category2 ):
587
+ permutations = False
588
+ return permutations
589
+
572
590
def _concat (self , to_concat : list [Index ], name : Hashable ) -> Index :
573
591
# if calling index is category, don't check dtype of others
574
592
575
- try :
593
+ cat_index = True
576
594
577
- result = Categorical ._concat_same_type (list (to_concat ))
595
+ for i in to_concat :
596
+ if not isinstance (i , CategoricalIndex ):
597
+ cat_index = False
598
+ break
578
599
579
- return CategoricalIndex (result , name = name )
600
+ try :
601
+ if cat_index :
602
+ if self ._permutation_check (to_concat ):
603
+ raise TypeError
604
+
605
+ codes = np .concatenate ([self ._is_dtype_compat (c ).codes for c in to_concat ])
580
606
581
607
except TypeError :
582
608
# not all to_concat elements are among our categories (or NA)
@@ -585,3 +611,7 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
585
611
res = concat_compat ([x ._values for x in to_concat ])
586
612
587
613
return Index (res , name = name )
614
+ else :
615
+
616
+ cat = self ._data ._from_backing_data (codes )
617
+ return type (self )._simple_new (cat , name = name )
0 commit comments