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 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
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 @@ -507,6 +507,7 @@ Reshaping
- Bug in :meth:`DataFrame.agg` with ``func={'name':<FUNC>}`` incorrectly raising ``TypeError`` when ``DataFrame.columns==['Name']`` (:issue:`36212`)
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
- Bug in :func:`join` returned a non deterministic level-order for the resulting :class:`MultiIndex` (:issue:`36910`)
- 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 @@ -1336,7 +1336,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 @@ -1861,8 +1861,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
10 changes: 10 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2283,3 +2283,13 @@ def test_merge_join_categorical_multiindex():
expected = expected.drop(["Cat", "Int"], axis=1)
result = a.join(b, on=["Cat1", "Int1"])
tm.assert_frame_equal(expected, result)


@pytest.mark.parametrize("how", ["left", "right"])
Copy link
Contributor

Choose a reason for hiding this comment

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

sort=True is ok? (do we test this)? can you locate this test function near similar tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sort True was the previous default behavior. Added tests to show behavior. Moved the tests up a bit right below a nosort test

def test_merge_same_order_left_right(how):
# GH: 35382
df = DataFrame({"a": [1, 0, 1]})

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