Skip to content

Commit 6cff6ed

Browse files
Only coerce to object if the calling index is not categorical
1 parent 9b76f7e commit 6cff6ed

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

pandas/indexes/base.py

+5
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,11 @@ def append(self, other):
14641464
names = set([obj.name for obj in to_concat])
14651465
name = None if len(names) > 1 else self.name
14661466

1467+
if self.is_categorical()
1468+
# if any of the to_concat is category
1469+
from pandas.indexes.category import CategoricalIndex
1470+
return CategoricalIndex._append_same_dtype(self, to_concat, name)
1471+
14671472
typs = _concat.get_dtype_kinds(to_concat)
14681473

14691474
if len(typs) == 1:

pandas/tests/indexes/test_category.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ def test_append(self):
272272

273273
# with objects
274274
result = ci.append(Index(['c', 'a']))
275-
#expected = CategoricalIndex(list('aabbcaca'), categories=categories)
276-
expected = Index(list('aabbcaca'))
275+
expected = CategoricalIndex(list('aabbcaca'), categories=categories)
277276
tm.assert_index_equal(result, expected, exact=True)
278277

279278
# invalid objects
280-
#self.assertRaises(TypeError, lambda: ci.append(Index(['a', 'd'])))
279+
self.assertRaises(TypeError, lambda: ci.append(Index(['a', 'd'])))
281280

282281
# GH14298 - if base object is not categorical -> coerce to object
283282
result = Index(['c', 'a']).append(ci)

0 commit comments

Comments
 (0)