Skip to content

Commit 8692863

Browse files
committed
1 parent 2fee99a commit 8692863

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pandas/core/indexes/category.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ def _format_attrs(self):
324324
"categories",
325325
ibase.default_pprint(self.categories, max_seq_items=max_categories),
326326
),
327-
("ordered", self.ordered),
327+
# pandas\core\indexes\category.py:315: error: "CategoricalIndex"
328+
# has no attribute "ordered" [attr-defined]
329+
("ordered", self.ordered), # type: ignore[attr-defined]
328330
]
329331
if self.name is not None:
330332
attrs.append(("name", ibase.default_pprint(self.name)))
@@ -659,6 +661,14 @@ def map(self, mapper):
659661

660662
def _concat(self, to_concat: List["Index"], name: Label) -> "CategoricalIndex":
661663
# if calling index is category, don't check dtype of others
664+
count = 0
665+
for c in to_concat:
666+
if not to_concat[0].ordered:
667+
if count != 0:
668+
to_concat[count] = CategoricalIndex(
669+
list(to_concat[count].values), list(to_concat[0].categories))
670+
# c.categories = ['a', 'b']
671+
count += 1
662672
codes = np.concatenate([self._is_dtype_compat(c).codes for c in to_concat])
663673
cat = self._data._from_backing_data(codes)
664674
return type(self)._simple_new(cat, name=name)

test_issue1.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pandas as pd
2+
3+
4+
cat1 = pd.CategoricalIndex(
5+
["a", "a",],
6+
categories=["a", "b"],
7+
ordered=False)
8+
cat2 = pd.CategoricalIndex(
9+
["b", "b"],
10+
categories=["b", "a"],
11+
ordered=False)
12+
13+
df1 = pd.DataFrame({"A": [1, 2]}, index=cat1)
14+
df2 = pd.DataFrame({"A": [3, 4]}, index=cat2)
15+
#print(df1)
16+
#print(df2)
17+
print(pd.concat((df1, df2))) # cat2 disappeared from index

0 commit comments

Comments
 (0)