Skip to content

PERF: MultiIndex.argsort / MultiIndex.sort_values #48406

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

Merged
merged 4 commits into from
Sep 9, 2022
Merged
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
15 changes: 15 additions & 0 deletions asv_bench/benchmarks/multiindex_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
MultiIndex,
RangeIndex,
Series,
array,
date_range,
)

Expand Down Expand Up @@ -176,6 +177,20 @@ def time_sortlevel_one(self):
self.mi.sortlevel(1)


class SortValues:

params = ["int64", "Int64"]
param_names = ["dtype"]

def setup(self, dtype):
a = array(np.tile(np.arange(100), 1000), dtype=dtype)
b = array(np.tile(np.arange(1000), 100), dtype=dtype)
self.mi = MultiIndex.from_arrays([a, b])

def time_sort_values(self, dtype):
self.mi.sort_values()


class Values:
def setup_cache(self):

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Deprecations
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Performance improvement in :meth:`.GroupBy.median` for nullable dtypes (:issue:`37493`)
- Performance improvement in :meth:`MultiIndex.argsort` and :meth:`MultiIndex.sort_values` (:issue:`48406`)
- Performance improvement in :meth:`.GroupBy.mean` and :meth:`.GroupBy.var` for extension array dtypes (:issue:`37493`)
- Performance improvement for :meth:`MultiIndex.unique` (:issue:`48335`)
-
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,10 @@ def append(self, other):
return Index._with_infer(new_tuples)

def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]:
if len(args) == 0 and len(kwargs) == 0:
# np.lexsort is significantly faster than self._values.argsort()
values = [self._get_level_values(i) for i in reversed(range(self.nlevels))]
return np.lexsort(values)
return self._values.argsort(*args, **kwargs)

@Appender(_index_shared_docs["repeat"] % _index_doc_kwargs)
Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,6 @@ def test_union_duplicates(index, request):
if index.empty or isinstance(index, (IntervalIndex, CategoricalIndex)):
# No duplicates in empty indexes
return
if index.dtype.kind == "c":
mark = pytest.mark.xfail(
reason="sort_values() call raises bc complex objects are not comparable"
)
request.node.add_marker(mark)

values = index.unique().values.tolist()
mi1 = MultiIndex.from_arrays([values, [1] * len(values)])
Expand Down