Skip to content

Commit e463818

Browse files
committed
Merge pull request #8948 from behzadnouri/left-merge
BUG: preserve left frame order in left merge
2 parents 2ad9dc7 + 327830a commit e463818

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Bug Fixes
101101
- ``slice`` string method now takes step into account (:issue:`8754`)
102102
- Bug in ``BlockManager`` where setting values with different type would break block integrity (:issue:`8850`)
103103
- Bug in ``DatetimeIndex`` when using ``time`` object as key (:issue:`8667`)
104+
- Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`)
104105
- Fix negative step support for label-based slices (:issue:`8753`)
105106
- Fix: The font size was only set on x axis if vertical or the y axis if horizontal. (:issue:`8765`)
106107

pandas/tools/merge.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,10 @@ def _get_join_indexers(left_keys, right_keys, sort=False, how='inner'):
479479
left_group_key, right_group_key, max_groups = \
480480
_factorize_keys(left_group_key, right_group_key, sort=sort)
481481

482+
# preserve left frame order if how == 'left' and sort == False
483+
kwargs = {'sort':sort} if how == 'left' else {}
482484
join_func = _join_functions[how]
483-
return join_func(left_group_key, right_group_key, max_groups)
485+
return join_func(left_group_key, right_group_key, max_groups, **kwargs)
484486

485487

486488
class _OrderedMerge(_MergeOperation):

pandas/tools/tests/test_merge.py

+12
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,13 @@ def test_left_join_index_multi_match_multiindex(self):
10221022

10231023
tm.assert_frame_equal(result, expected)
10241024

1025+
# GH7331 - maintain left frame order in left merge
1026+
right.reset_index(inplace=True)
1027+
right.columns = left.columns[:3].tolist() + right.columns[-1:].tolist()
1028+
result = merge(left, right, how='left', on=left.columns[:-1].tolist())
1029+
expected.index = np.arange(len(expected))
1030+
tm.assert_frame_equal(result, expected)
1031+
10251032
def test_left_join_index_multi_match(self):
10261033
left = DataFrame([
10271034
['c', 0],
@@ -1059,6 +1066,11 @@ def test_left_join_index_multi_match(self):
10591066

10601067
tm.assert_frame_equal(result, expected)
10611068

1069+
# GH7331 - maintain left frame order in left merge
1070+
result = merge(left, right.reset_index(), how='left', on='tag')
1071+
expected.index = np.arange(len(expected))
1072+
tm.assert_frame_equal(result, expected)
1073+
10621074
def test_join_multi_dtypes(self):
10631075

10641076
# test with multi dtypes in the join index

0 commit comments

Comments
 (0)