Skip to content

BUG/API: Index.append with mixed object/Categorical indices #14545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,13 +1464,13 @@ def append(self, other):
names = set([obj.name for obj in to_concat])
name = None if len(names) > 1 else self.name

typs = _concat.get_dtype_kinds(to_concat)

if 'category' in typs:
# if any of the to_concat is category
if self.is_categorical():
# if calling index is category, don't check dtype of others
from pandas.indexes.category import CategoricalIndex
return CategoricalIndex._append_same_dtype(self, to_concat, name)

typs = _concat.get_dtype_kinds(to_concat)

if len(typs) == 1:
return self._append_same_dtype(to_concat, name=name)
return _concat._concat_index_asobject(to_concat, name=name)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_repr_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,11 @@ def memory_usage(f):

# high upper bound
self.assertTrue(memory_usage(unstacked) - memory_usage(df) < 2000)

def test_info_categorical(self):
# GH14298
idx = pd.CategoricalIndex(['a', 'b'])
df = pd.DataFrame(np.zeros((2, 2)), index=idx, columns=idx)

buf = StringIO()
df.info(buf=buf)
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ def test_append(self):
# invalid objects
self.assertRaises(TypeError, lambda: ci.append(Index(['a', 'd'])))

# GH14298 - if base object is not categorical -> coerce to object
result = Index(['c', 'a']).append(ci)
expected = Index(list('caaabbca'))
tm.assert_index_equal(result, expected, exact=True)

def test_insert(self):

ci = self.create_index()
Expand Down