Skip to content

Commit 9ee8c0d

Browse files
chris-b1jreback
authored andcommitted
BUG: union_categorical fastpath sort
closes pandas-dev#13899 xref pandas-dev#13846
1 parent 61b14b2 commit 9ee8c0d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/tools/tests/test_concat.py

+7
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,13 @@ def test_union_categoricals_sort(self):
10061006
categories=['a', 'b', 'c'])
10071007
tm.assert_categorical_equal(result, expected)
10081008

1009+
c1 = Categorical(['a', 'b'], categories=['c', 'a', 'b'])
1010+
c2 = Categorical(['b', 'c'], categories=['c', 'a', 'b'])
1011+
result = union_categoricals([c1, c2], sort_categories=True)
1012+
expected = Categorical(['a', 'b', 'b', 'c'],
1013+
categories=['a', 'b', 'c'])
1014+
tm.assert_categorical_equal(result, expected)
1015+
10091016
# fastpath - skip resort
10101017
c1 = Categorical(['a', 'b'], categories=['a', 'b', 'c'])
10111018
c2 = Categorical(['b', 'c'], categories=['a', 'b', 'c'])

pandas/types/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def union_categoricals(to_union, sort_categories=False):
263263

264264
if sort_categories and not categories.is_monotonic_increasing:
265265
categories = categories.sort_values()
266-
indexer = first.categories.get_indexer(categories)
266+
indexer = categories.get_indexer(first.categories)
267267
new_codes = take_1d(indexer, new_codes, fill_value=-1)
268268
elif all(not c.ordered for c in to_union):
269269
# different categories - union and recode

0 commit comments

Comments
 (0)