Skip to content

Commit 78fbc2c

Browse files
committed
BUG: Raise TypeError when joining with non-DataFrame using 'on=' (GH#61434)
1 parent 5b0767a commit 78fbc2c

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ Other
880880
- Bug in :meth:`DataFrame.apply` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`)
881881
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`)
882882
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`)
883+
- Bug in :meth:`DataFrame.join` where passing a non-pandas object like a ``polars.DataFrame`` with the ``on=`` parameter raised a misleading error message instead of a ``TypeError``. (:issue:`61434`)
883884
- Bug in :meth:`DataFrame.query` where using duplicate column names led to a ``TypeError``. (:issue:`59950`)
884885
- Bug in :meth:`DataFrame.query` which raised an exception or produced incorrect results when expressions contained backtick-quoted column names containing the hash character ``#``, backticks, or characters that fall outside the ASCII range (U+0001..U+007F). (:issue:`59285`) (:issue:`49633`)
885886
- Bug in :meth:`DataFrame.query` which raised an exception when querying integer column names using backticks. (:issue:`60494`)

pandas/core/frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -10885,6 +10885,12 @@ def join(
1088510885
raise ValueError("Other Series must have a name")
1088610886
other = DataFrame({other.name: other})
1088710887

10888+
if on is not None and not isinstance(other, (DataFrame, Series)):
10889+
raise TypeError(
10890+
f"Join with 'on=...' requires a pandas DataFrame or Series as 'other', "
10891+
f"got {type(other).__name__} instead."
10892+
)
10893+
1088810894
if isinstance(other, DataFrame):
1088910895
if how == "cross":
1089010896
return merge(

pandas/tests/frame/methods/test_join.py

+23
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,29 @@ def test_suppress_future_warning_with_sort_kw(sort):
418418
tm.assert_frame_equal(result, expected)
419419

420420

421+
def test_join_with_non_pandas_object_raises_typeerror():
422+
# GH#61434
423+
df1 = DataFrame(
424+
{
425+
"Column2": [10, 20, 30],
426+
"Column3": ["A", "B", "C"],
427+
"Column4": ["Lala", "YesYes", "NoNo"],
428+
}
429+
)
430+
431+
class FakeOther:
432+
def __init__(self):
433+
self.Column2 = [10, 20, 30]
434+
self.Column3 = ["A", "B", "C"]
435+
436+
other = FakeOther()
437+
438+
with pytest.raises(
439+
TypeError, match="Join with 'on=.*' requires a pandas DataFrame or Series"
440+
):
441+
df1.join(other, on=["Column2", "Column3"], how="inner")
442+
443+
421444
class TestDataFrameJoin:
422445
def test_join(self, multiindex_dataframe_random_data):
423446
frame = multiindex_dataframe_random_data

0 commit comments

Comments
 (0)