Skip to content

Commit 070fdc0

Browse files
authored
BUG: join changes index type when doing join on empty dataframes (#53893)
* BUG: join changes index type when doing join on empty dataframes * move to v2.1.0
1 parent b5d666c commit 070fdc0

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ Reshaping
524524
- Bug in :meth:`DataFrame.stack` sorting index lexicographically in rare cases (:issue:`53824`)
525525
- Bug in :meth:`DataFrame.transpose` inferring dtype for object column (:issue:`51546`)
526526
- Bug in :meth:`Series.combine_first` converting ``int64`` dtype to ``float64`` and losing precision on very large integers (:issue:`51764`)
527+
- Bug when joining empty :class:`DataFrame` objects, where the joined index would be a :class:`RangeIndex` instead of the joined index type (:issue:`52777`)
527528
-
528529

529530
Sparse

pandas/core/reshape/merge.py

-2
Original file line numberDiff line numberDiff line change
@@ -1157,8 +1157,6 @@ def _get_join_info(
11571157
else:
11581158
join_index = default_index(len(left_indexer))
11591159

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

11641162
@final

pandas/tests/reshape/merge/test_merge.py

+9
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ def test_left_merge_empty_dataframe(self):
438438
result = merge(right, left, on="key", how="right")
439439
tm.assert_frame_equal(result, left)
440440

441+
@pytest.mark.parametrize("how", ["inner", "left", "right", "outer"])
442+
def test_merge_empty_dataframe(self, index, how):
443+
# GH52777
444+
left = DataFrame([], index=index[:0])
445+
right = left.copy()
446+
447+
result = left.join(right, how=how)
448+
tm.assert_frame_equal(result, left)
449+
441450
@pytest.mark.parametrize(
442451
"kwarg",
443452
[

0 commit comments

Comments
 (0)