Skip to content

Commit dca658b

Browse files
authored
PERF: Join non unique (#56817)
* PERF: join non-unique * whatsnew
1 parent fa5e835 commit dca658b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

doc/source/whatsnew/v2.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Deprecations
101101

102102
Performance improvements
103103
~~~~~~~~~~~~~~~~~~~~~~~~
104+
- Performance improvement in :meth:`DataFrame.join` when left and/or right are non-unique and ``how`` is ``"left"``, ``"right"``, or ``"inner"`` (:issue:`56817`)
104105
- Performance improvement in :meth:`Index.take` when ``indices`` is a full range indexer from zero to length of index (:issue:`56806`)
105106
-
106107

pandas/core/indexes/base.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -4809,11 +4809,18 @@ def _join_non_unique(
48094809
left_idx, right_idx = get_join_indexers_non_unique(
48104810
self._values, other._values, how=how, sort=sort
48114811
)
4812-
mask = left_idx == -1
48134812

4814-
join_idx = self.take(left_idx)
4815-
right = other.take(right_idx)
4816-
join_index = join_idx.putmask(mask, right)
4813+
if how == "right":
4814+
join_index = other.take(right_idx)
4815+
else:
4816+
join_index = self.take(left_idx)
4817+
4818+
if how == "outer":
4819+
mask = left_idx == -1
4820+
if mask.any():
4821+
right = other.take(right_idx)
4822+
join_index = join_index.putmask(mask, right)
4823+
48174824
if isinstance(join_index, ABCMultiIndex) and how == "outer":
48184825
# test_join_index_levels
48194826
join_index = join_index._sort_levels_monotonic()

0 commit comments

Comments
 (0)