From 86928636178c14c6411d968b03324d663eedf3ee Mon Sep 17 00:00:00 2001 From: shuyan Date: Mon, 14 Dec 2020 20:33:38 +0800 Subject: [PATCH] GH #24845 --- pandas/core/indexes/category.py | 12 +++++++++++- test_issue1.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test_issue1.py diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 1be979b1b899c..6ebc8d15d66d7 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -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))) @@ -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) diff --git a/test_issue1.py b/test_issue1.py new file mode 100644 index 0000000000000..7d57c2e5d5793 --- /dev/null +++ b/test_issue1.py @@ -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 \ No newline at end of file