Skip to content

Commit ed54bec

Browse files
author
Nico Cernek
committed
swap left and right keys when how == "right"
1 parent 8d92f8a commit ed54bec

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

pandas/core/reshape/merge.py

+13-28
Original file line numberDiff line numberDiff line change
@@ -1283,34 +1283,39 @@ def _get_join_indexers(left_keys, right_keys, sort=False, how="inner", **kwargs)
12831283
indexers into the left_keys, right_keys
12841284
12851285
"""
1286+
_how = how
1287+
if how == "right":
1288+
left_keys, right_keys = right_keys, left_keys
1289+
_how = "left"
1290+
12861291
assert len(left_keys) == len(
12871292
right_keys
12881293
), "left_key and right_keys must be the same length"
12891294

12901295
# bind `sort` arg. of _factorize_keys
12911296
fkeys = partial(_factorize_keys, sort=sort)
1292-
print(left_keys, right_keys)
12931297
# get left & right join labels and num. of levels at each location
12941298
llab, rlab, shape = map(list, zip(*map(fkeys, left_keys, right_keys)))
12951299

12961300
# get flat i8 keys from label lists
1297-
print(llab, rlab)
12981301
lkey, rkey = _get_join_keys(llab, rlab, shape, sort)
12991302

13001303
# factorize keys to a dense i8 space
13011304
# `count` is the num. of unique keys
13021305
# set(lkey) | set(rkey) == range(count)
1303-
1304-
print(lkey, rkey)
13051306
lkey, rkey, count = fkeys(lkey, rkey)
1306-
print(lkey, rkey)
13071307
# preserve left frame order if how == 'left' and sort == False
13081308
kwargs = copy.copy(kwargs)
1309-
if how == "left":
1309+
if _how == "left":
13101310
kwargs["sort"] = sort
1311-
join_func = _join_functions[how]
1311+
join_func = _join_functions[_how]
1312+
1313+
left_indexer, right_indexer = join_func(lkey, rkey, count, **kwargs)
13121314

1313-
return join_func(lkey, rkey, count, **kwargs)
1315+
if how == "right":
1316+
left_indexer, right_indexer = right_indexer, left_indexer
1317+
1318+
return left_indexer, right_indexer
13141319

13151320

13161321
def _restore_dropped_levels_multijoin(
@@ -1831,29 +1836,9 @@ def _left_join_on_index(left_ax, right_ax, join_keys, sort=False):
18311836
return left_ax, None, right_indexer
18321837

18331838

1834-
def _right_outer_join(x, y, max_groups):
1835-
new_x = []
1836-
for i in y:
1837-
if i in x:
1838-
new_x.append(i)
1839-
else:
1840-
new_x.append(-1)
1841-
1842-
return np.array(new_x), np.array([0, 1, 2])
1843-
# right_indexer, left_indexer = libjoin.left_outer_join(y, x, max_groups)
1844-
# print('right_index: ', y, " - ", right_indexer)
1845-
# print('left_index: ', x, " - ", left_indexer)
1846-
1847-
# assert np.array_equal(left_indexer, np.array([1, 2, -1]))
1848-
# assert np.array_equal(right_indexer, np.array([1, 2, 0]))
1849-
# return np.array([-1, 1, 2]), np.array([0,1,2])
1850-
# return left_indexer, right_indexer
1851-
1852-
18531839
_join_functions = {
18541840
"inner": libjoin.inner_join,
18551841
"left": libjoin.left_outer_join,
1856-
"right": _right_outer_join,
18571842
"outer": libjoin.full_outer_join,
18581843
}
18591844

0 commit comments

Comments
 (0)