From 1fac76ba9b3956c1a9bf6ad7f2c5ee13e2949237 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Wed, 28 Jun 2023 00:22:00 +0100 Subject: [PATCH 1/2] BUG: join changes index type when doing join on empty dataframes --- doc/source/whatsnew/v2.0.3.rst | 1 + pandas/core/reshape/merge.py | 2 -- pandas/tests/reshape/merge/test_merge.py | 9 +++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.0.3.rst b/doc/source/whatsnew/v2.0.3.rst index 14e65e9e577ce..5e4c0981e7b85 100644 --- a/doc/source/whatsnew/v2.0.3.rst +++ b/doc/source/whatsnew/v2.0.3.rst @@ -14,6 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Bug in :meth:`Timestamp.weekday`` was returning incorrect results before ``'0000-02-29'`` (:issue:`53738`) +- Bug when joining empty :class:`DataFrame` objects, where the joined index would be a :class:`RangeIndex` instead of the joined index type (:issue:`52777`) - Fixed performance regression in merging on datetime-like columns (:issue:`53231`) - Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) - For external ExtensionArray implementations, restored the default use of ``_values_for_factorize`` for hashing arrays (:issue:`53475`) 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", [ From 8c454ae6bb331c2fb54bd1cb11bb68b0d4653e07 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Fri, 7 Jul 2023 23:55:57 +0100 Subject: [PATCH 2/2] move to v2.1.0 --- doc/source/whatsnew/v2.0.3.rst | 1 - doc/source/whatsnew/v2.1.0.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.3.rst b/doc/source/whatsnew/v2.0.3.rst index 18af95b2ad8b8..3e2dc1b92c779 100644 --- a/doc/source/whatsnew/v2.0.3.rst +++ b/doc/source/whatsnew/v2.0.3.rst @@ -14,7 +14,6 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Bug in :meth:`Timestamp.weekday`` was returning incorrect results before ``'0000-02-29'`` (:issue:`53738`) -- Bug when joining empty :class:`DataFrame` objects, where the joined index would be a :class:`RangeIndex` instead of the joined index type (:issue:`52777`) - Fixed performance regression in merging on datetime-like columns (:issue:`53231`) - Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) 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