Skip to content

Commit ee9f872

Browse files
committed
faster _index_level_unique_labels by using np.unique before converting to "object"/python type
1 parent 4df3644 commit ee9f872

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

larray/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,13 @@ def _index_level_unique_labels(idx, level):
756756
# * if using .labels[level].values() gets unsupported at one point,
757757
# simply use "unique_values = set(idx.get_level_values(level))" instead
758758

759+
level_num = idx._get_level_number(level)
759760
# .values() to get a straight ndarray from the FrozenNDArray that .labels[]
760761
# gives us, which is slower to iterate on
761762
# .astype(object) because set() needs python objects and it is faster to
762763
# convert all ints in bulk than having them converted in the array iterator
763-
level_num = idx._get_level_number(level)
764-
unique_labels = set(idx.labels[level_num].values().astype(object))
764+
# (it only pays for itself with len(unique) > ~100)
765+
unique_labels = set(np.unique(idx.labels[level_num].values())
766+
.astype(object))
765767
order = idx.levels[level_num]
766768
return [v for i, v in enumerate(order) if i in unique_labels]

0 commit comments

Comments
 (0)