diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 198a7155e1a1e..d4f25e159bdfb 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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 diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index e68277c38063e..ea2bb9db83373 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -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 diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index 896f1a9be52be..fac579f9935c1 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -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", [