Skip to content

BUG: merge between partial index and index fails when result is empty #34414

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

Merged
Show file tree
Hide file tree
Changes from 6 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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ Reshaping
- Bug in :meth:`DataFrame.replace` casts columns to ``object`` dtype if items in ``to_replace`` not in values (:issue:`32988`)
- Ensure only named functions can be used in :func:`eval()` (:issue:`32460`)
- Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`)
- Fixed bug in :func:`merge` where an error was raised when performing an inner join with partial index and ``right_index`` when no overlap between indices (:issue:`33814`)

Sparse
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def _maybe_add_join_keys(self, result, left_indexer, right_indexer):
# make sure to just use the right values
mask = left_indexer == -1
if mask.all():
key_col = rvals
key_col = Index(rvals)
else:
key_col = Index(lvals).where(~mask, rvals)

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2227,3 +2227,21 @@ def test_categorical_non_unique_monotonic(n_categories):
index=left_index,
)
tm.assert_frame_equal(expected, result)


@pytest.mark.parametrize(
("kwargs", "args"),
[
({"left_on": ["b"], "right_index": True}, 1),
({"left_index": True, "right_on": ["b"]}, -1),
],
)
def test_merge_empty_right_index_left_on(kwargs, args):
# GH 33814
df1 = pd.DataFrame({"a": [1], "b": [2]}).set_index(["a", "b"])
df2 = pd.DataFrame({"b": [1]}).set_index(["b"])
result = pd.merge(*[df1, df2][::args], **kwargs)
expected = pd.DataFrame(
index=MultiIndex(levels=[[int()], [int()]], codes=[[], []], names=["a", "b"])
)
tm.assert_frame_equal(result, expected)