Skip to content
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

BUG: Fix inconsistent ordering between left and right in merge #37406

Merged
merged 9 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ Reshaping
- Bug in :func:`join` returned a non deterministic level-order for the resulting :class:`MultiIndex` (:issue:`36910`)
- Bug in :meth:`DataFrame.combine_first()` caused wrong alignment with dtype ``string`` and one level of ``MultiIndex`` containing only ``NA`` (:issue:`37591`)
- Fixed regression in :func:`merge` on merging DatetimeIndex with empty DataFrame (:issue:`36895`)
- Bug in :meth:`df.merge() <pandas.DataFrame.merge>` returned inconsistent ordering in result for ``how=right`` and ``how=left`` (:issue:`35382`)

Sparse
^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ def get_join_indexers(
lkey, rkey, count = _factorize_keys(lkey, rkey, sort=sort, how=how)
# preserve left frame order if how == 'left' and sort == False
kwargs = copy.copy(kwargs)
if how == "left":
if how in ("left", "right"):
kwargs["sort"] = sort
join_func = {
"inner": libjoin.inner_join,
Expand Down Expand Up @@ -1864,8 +1864,8 @@ def _left_join_on_index(left_ax: Index, right_ax: Index, join_keys, sort: bool =
return left_ax, None, right_indexer


def _right_outer_join(x, y, max_groups):
jreback marked this conversation as resolved.
Show resolved Hide resolved
right_indexer, left_indexer = libjoin.left_outer_join(y, x, max_groups)
def _right_outer_join(x, y, max_groups, **kwargs):
right_indexer, left_indexer = libjoin.left_outer_join(y, x, max_groups, **kwargs)
return left_indexer, right_indexer


Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,18 @@ def test_merge_nosort(self):

assert (df.var3.unique() == result.var3.unique()).all()

@pytest.mark.parametrize(
("sort", "values"), [(False, [1, 1, 0, 1, 1]), (True, [0, 1, 1, 1, 1])]
)
@pytest.mark.parametrize("how", ["left", "right"])
def test_merge_same_order_left_right(self, sort, values, how):
# GH: 35382
df = DataFrame({"a": [1, 0, 1]})

result = df.merge(df, on="a", how=how, sort=sort)
expected = DataFrame(values, columns=["a"])
tm.assert_frame_equal(result, expected)

def test_merge_nan_right(self):
df1 = DataFrame({"i1": [0, 1], "i2": [0, 1]})
df2 = DataFrame({"i1": [0], "i3": [0]})
Expand Down