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: merge between partial index and index fails when result is empty #34414

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.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 result was empty (:issue:`33814`)
phofl marked this conversation as resolved.
Show resolved Hide resolved

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)
phofl marked this conversation as resolved.
Show resolved Hide resolved
else:
key_col = Index(lvals).where(~mask, rvals)

Expand Down
7 changes: 7 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,10 @@ def test_categorical_non_unique_monotonic(n_categories):
index=left_index,
)
tm.assert_frame_equal(expected, result)


def test_merge_empty_right_index_left_on():
Copy link
Contributor

Choose a reason for hiding this comment

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

can you do the exact reverse as well and parameterize over it (e.g. left_index=True)

Copy link
Member Author

Choose a reason for hiding this comment

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

We are already testing both cases, parametrization is not really feasible here because we have to switch the dataframes too.
Renamed the test.

left = pd.DataFrame({"a": [1], "b": [2]}).set_index(["a", "b"])
phofl marked this conversation as resolved.
Show resolved Hide resolved
right = pd.DataFrame({"b": [1]}).set_index(["b"])
result = pd.merge(left, right, left_on=["b"], right_index=True)
phofl marked this conversation as resolved.
Show resolved Hide resolved
assert result.empty
phofl marked this conversation as resolved.
Show resolved Hide resolved