Skip to content

Commit 9345aaa

Browse files
committed
PERF: building MultiIndex with categorical levels
1 parent 9a67ff4 commit 9345aaa

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ Performance Improvements
514514
- Improved performance of :meth:`read_csv` by faster concatenating date columns without extra conversion to string for integer/float zero and float ``NaN``; by faster checking the string for the possibility of being a date (:issue:`25754`)
515515
- Improved performance of :attr:`IntervalIndex.is_unique` by removing conversion to ``MultiIndex`` (:issue:`24813`)
516516
- Restored performance of :meth:`DatetimeIndex.__iter__` by re-enabling specialized code path (:issue:`26702`)
517+
- Improved performance of :meth:`DataFrame.set_index` when using multiple indexes and at least one of them is a categorical object (:issue:`22044`)
517518

518519
.. _whatsnew_0250.bug_fixes:
519520

pandas/core/arrays/categorical.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2666,9 +2666,9 @@ def _factorize_from_iterable(values):
26662666
raise TypeError("Input must be list-like")
26672667

26682668
if is_categorical(values):
2669-
if isinstance(values, (ABCCategoricalIndex, ABCSeries)):
2670-
values = values._values
2671-
categories = CategoricalIndex(values.categories, dtype=values.dtype)
2669+
values = CategoricalIndex(values)
2670+
cat_codes = np.arange(len(values.categories), dtype=values.codes.dtype)
2671+
categories = values._create_from_codes(cat_codes)
26722672
codes = values.codes
26732673
else:
26742674
# The value of ordered is irrelevant since we don't use cat as such,

0 commit comments

Comments
 (0)