Skip to content

Commit eea1777

Browse files
committed
skip r-esort when possible on fastpath
1 parent c559662 commit eea1777

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pandas/tools/tests/test_concat.py

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

1009+
# fastpath - skip resort
1010+
c1 = Categorical(['a', 'b'], categories=['a', 'b', 'c'])
1011+
c2 = Categorical(['b', 'c'], categories=['a', 'b', 'c'])
1012+
result = union_categoricals([c1, c2], sort_categories=True)
1013+
expected = Categorical(['a', 'b', 'b', 'c'],
1014+
categories=['a', 'b', 'c'])
1015+
tm.assert_categorical_equal(result, expected)
1016+
10091017
c1 = Categorical(['x', np.nan])
10101018
c2 = Categorical([np.nan, 'b'])
10111019
result = union_categoricals([c1, c2], sort_categories=True)

pandas/types/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def union_categoricals(to_union, sort_categories=False):
256256
ordered = first.ordered
257257
new_codes = np.concatenate([c.codes for c in to_union])
258258

259-
if sort_categories:
259+
if sort_categories and not categories.is_monotonic_increasing:
260260
categories = categories.sort_values()
261261
indexer = first.categories.get_indexer(categories)
262262
new_codes = take_1d(indexer, new_codes, fill_value=-1)

0 commit comments

Comments
 (0)