Skip to content

BUG: union_categorical fastpath sort #13899

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

Closed
Closed
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
7 changes: 7 additions & 0 deletions pandas/tools/tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,13 @@ def test_union_categoricals_sort(self):
categories=['a', 'b', 'c'])
tm.assert_categorical_equal(result, expected)

c1 = Categorical(['a', 'b'], categories=['c', 'a', 'b'])
c2 = Categorical(['b', 'c'], categories=['c', 'a', 'b'])
result = union_categoricals([c1, c2], sort_categories=True)
expected = Categorical(['a', 'b', 'b', 'c'],
categories=['a', 'b', 'c'])
tm.assert_categorical_equal(result, expected)

# fastpath - skip resort
c1 = Categorical(['a', 'b'], categories=['a', 'b', 'c'])
c2 = Categorical(['b', 'c'], categories=['a', 'b', 'c'])
Expand Down
2 changes: 1 addition & 1 deletion pandas/types/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def union_categoricals(to_union, sort_categories=False):

if sort_categories and not categories.is_monotonic_increasing:
categories = categories.sort_values()
indexer = first.categories.get_indexer(categories)
indexer = categories.get_indexer(first.categories)
new_codes = take_1d(indexer, new_codes, fill_value=-1)
elif all(not c.ordered for c in to_union):
# different categories - union and recode
Expand Down