Skip to content

Commit 240cb8e

Browse files
committed
updated the code in accordance with GH24845
1 parent 9e7a7bd commit 240cb8e

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

pandas/core/indexes/category.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,40 @@ def map(self, mapper):
569569
mapped = self._values.map(mapper)
570570
return Index(mapped, name=self.name)
571571

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+
572590
def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
573591
# if calling index is category, don't check dtype of others
574592

575-
try:
593+
cat_index = True
576594

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
578599

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])
580606

581607
except TypeError:
582608
# 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:
585611
res = concat_compat([x._values for x in to_concat])
586612

587613
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

Comments
 (0)