Skip to content

BUG: join changes index type when doing join on empty dataframes #53893

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
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ Reshaping
- Bug in :meth:`DataFrame.stack` sorting index lexicographically in rare cases (:issue:`53824`)
- Bug in :meth:`DataFrame.transpose` inferring dtype for object column (:issue:`51546`)
- Bug in :meth:`Series.combine_first` converting ``int64`` dtype to ``float64`` and losing precision on very large integers (:issue:`51764`)
- Bug when joining empty :class:`DataFrame` objects, where the joined index would be a :class:`RangeIndex` instead of the joined index type (:issue:`52777`)
-

Sparse
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,6 @@ def _get_join_info(
else:
join_index = default_index(len(left_indexer))

if len(join_index) == 0 and not isinstance(join_index, MultiIndex):
join_index = default_index(0).set_names(join_index.name)
return join_index, left_indexer, right_indexer

@final
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ def test_left_merge_empty_dataframe(self):
result = merge(right, left, on="key", how="right")
tm.assert_frame_equal(result, left)

@pytest.mark.parametrize("how", ["inner", "left", "right", "outer"])
def test_merge_empty_dataframe(self, index, how):
# GH52777
left = DataFrame([], index=index[:0])
right = left.copy()

result = left.join(right, how=how)
tm.assert_frame_equal(result, left)

@pytest.mark.parametrize(
"kwarg",
[
Expand Down