Skip to content

PERF: join/merge on subset of MultiIndex #48611

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 6 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions asv_bench/benchmarks/join_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ def time_left_outer_join_index(self):
self.left.join(self.right, on="jim")


class JoinMultiindexSubset:
def setup(self):
N = 100_000
mi1 = MultiIndex.from_arrays([np.arange(N)] * 4, names=["a", "b", "c", "d"])
mi2 = MultiIndex.from_arrays([np.arange(N)] * 2, names=["a", "b"])
self.left = DataFrame({"col1": 1}, index=mi1)
self.right = DataFrame({"col2": 2}, index=mi2)

def time_join_multiindex_subset(self):
self.left.join(self.right)


class JoinEmpty:
def setup(self):
N = 100_000
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 @@ -110,6 +110,7 @@ Performance improvements
- Performance improvement for :meth:`Series.value_counts` with nullable dtype (:issue:`48338`)
- Performance improvement for :class:`Series` constructor passing integer numpy array with nullable dtype (:issue:`48338`)
- Performance improvement for :meth:`MultiIndex.unique` (:issue:`48335`)
- Performance improvement in :meth:`DataFrame.join` when joining on a subset of a :class:`MultiIndex` (:issue:`48611`)
- Performance improvement in ``var`` for nullable dtypes (:issue:`48379`).
- Performance improvement to :func:`read_sas` with ``blank_missing=True`` (:issue:`48502`)
-
Expand Down
16 changes: 7 additions & 9 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,21 +1678,19 @@ def _convert_to_multiindex(index: Index) -> MultiIndex:
join_codes = join_index.codes
join_names = join_index.names

# lindexer and rindexer hold the indexes where the join occurred
# for left and right respectively. If left/right is None then
# the join occurred on all indices of left/right
if lindexer is None:
lindexer = range(left.size)

if rindexer is None:
rindexer = range(right.size)

# Iterate through the levels that must be restored
for dropped_level_name in dropped_level_names:
# lindexer and rindexer hold the indexes where the join occurred
# for left and right respectively. If left/right is None then
# the join occurred on all indices of left/right
if dropped_level_name in left.names:
if lindexer is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you pulling this inside the loop? This is harder to read than before

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We can actually avoid calling take_nd when the indexers are None as that means "take everything". I made another commit which further improves times and should be clearer.

       before           after         ratio
     [a712c501]       [ad3f42b5]
     <main>           <multiindex-join-subset>
-    43.8±1ms         10.5±1ms        0.24  join_merge.JoinMultiindexSubset.time_join_multiindex_subset

lindexer = np.arange(left.size)
idx = left
indexer = lindexer
else:
if rindexer is None:
rindexer = np.arange(right.size)
idx = right
indexer = rindexer

Expand Down