diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index d2d5707f32bf38..8b3cfc4193c0fe 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -334,6 +334,7 @@ Bug fixes - Fixed bug in :meth:`.DataFrameGroupBy.median` where nat values gave an incorrect result. (:issue:`57926`) - Fixed bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`) - Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`) +- Fixed bug in :meth:`DataFrame.join` join with a list of a single element should behave as a join with a single element (:issue:`57676`) - Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`) - Fixed bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`) - Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 66a68755a2a09e..f0d1cb6479714f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10587,6 +10587,9 @@ def join( from pandas.core.reshape.concat import concat from pandas.core.reshape.merge import merge + if isinstance(other, list) and len(other) == 1: + other = other[0] + if isinstance(other, Series): if other.name is None: raise ValueError("Other Series must have a name") diff --git a/pandas/tests/frame/methods/test_join.py b/pandas/tests/frame/methods/test_join.py index 82802dd6e99eb4..77d7b0a505879b 100644 --- a/pandas/tests/frame/methods/test_join.py +++ b/pandas/tests/frame/methods/test_join.py @@ -107,8 +107,6 @@ def test_suffix_on_list_join(): # check proper errors are raised msg = "Suffixes not supported when joining multiple DataFrames" - with pytest.raises(ValueError, match=msg): - first.join([second], lsuffix="y") with pytest.raises(ValueError, match=msg): first.join([second, third], rsuffix="x") with pytest.raises(ValueError, match=msg): @@ -562,3 +560,21 @@ def test_frame_join_tzaware(self): tm.assert_index_equal(result.index, expected) assert result.index.tz.zone == "US/Central" + + def test_join_list_with_single_element(self): + test1 = DataFrame( + {"cat": pd.Categorical(["a", "v", "d"])}, + index=Index(["a", "b", "c"], name="y"), + ) + test2 = DataFrame( + {"foo": np.arange(6)}, + index=MultiIndex.from_tuples( + [(0, "a"), (0, "b"), (0, "c"), (1, "a"), (1, "b"), (1, "c")], + names=("x", "y"), + ), + ) + + result = test2.join([test1]) + expected = test2.join(test1) + + tm.assert_frame_equal(result, expected)