Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
YuRenee committed Dec 14, 2020
1 parent 2fee99a commit 8692863
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ def _format_attrs(self):
"categories",
ibase.default_pprint(self.categories, max_seq_items=max_categories),
),
("ordered", self.ordered),
# pandas\core\indexes\category.py:315: error: "CategoricalIndex"
# has no attribute "ordered" [attr-defined]
("ordered", self.ordered), # type: ignore[attr-defined]
]
if self.name is not None:
attrs.append(("name", ibase.default_pprint(self.name)))
Expand Down Expand Up @@ -659,6 +661,14 @@ def map(self, mapper):

def _concat(self, to_concat: List["Index"], name: Label) -> "CategoricalIndex":
# if calling index is category, don't check dtype of others
count = 0
for c in to_concat:
if not to_concat[0].ordered:
if count != 0:
to_concat[count] = CategoricalIndex(
list(to_concat[count].values), list(to_concat[0].categories))
# c.categories = ['a', 'b']
count += 1
codes = np.concatenate([self._is_dtype_compat(c).codes for c in to_concat])
cat = self._data._from_backing_data(codes)
return type(self)._simple_new(cat, name=name)
Expand Down
17 changes: 17 additions & 0 deletions test_issue1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pandas as pd


cat1 = pd.CategoricalIndex(
["a", "a",],
categories=["a", "b"],
ordered=False)
cat2 = pd.CategoricalIndex(
["b", "b"],
categories=["b", "a"],
ordered=False)

df1 = pd.DataFrame({"A": [1, 2]}, index=cat1)
df2 = pd.DataFrame({"A": [3, 4]}, index=cat2)
#print(df1)
#print(df2)
print(pd.concat((df1, df2))) # cat2 disappeared from index

0 comments on commit 8692863

Please sign in to comment.