Skip to content

BUG: Raise TypeError when joining with non-DataFrame using 'on=' (GH#61434) #61454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ Reshaping
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
- 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`)

Sparse
^^^^^^
Expand Down
17 changes: 17 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10885,6 +10885,23 @@ def join(
raise ValueError("Other Series must have a name")
other = DataFrame({other.name: other})

if on is not None:
if isinstance(other, Iterable) and not isinstance(
other, (DataFrame, Series, str, bytes, bytearray)
):
Comment on lines +10889 to +10891
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(other, Iterable) and not isinstance(
other, (DataFrame, Series, str, bytes, bytearray)
):
if is_list_like(other):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this probably should be done in merge

if not all(isinstance(obj, (DataFrame, Series)) for obj in other):
raise TypeError(
f"Join with 'on={on}' requires a pandas DataFrame or Series, "
"or an iterable of such objects as 'other'. "
f"Got {type(other).__name__} with invalid elements."
)
elif not isinstance(other, (DataFrame, Series)):
raise TypeError(
f"Join with 'on={on}' requires a pandas DataFrame or Series as "
"'other'. Got "
f"{type(other).__name__} instead."
)

if isinstance(other, DataFrame):
if how == "cross":
return merge(
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/frame/methods/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,35 @@ def test_suppress_future_warning_with_sort_kw(sort):
tm.assert_frame_equal(result, expected)


def test_join_with_invalid_non_pandas_objects_raises_typeerror():
# GH#61434
# case - 'other' is an invalid non-pandas object
df1 = DataFrame(
{
"Column2": [10, 20, 30],
"Column3": ["A", "B", "C"],
"Column4": ["Lala", "YesYes", "NoNo"],
}
)

class FakeOther:
def __init__(self):
self.Column2 = [10, 20, 30]
self.Column3 = ["A", "B", "C"]

invalid_other = FakeOther()

with pytest.raises(TypeError, match="requires a pandas DataFrame or Series"):
df1.join(invalid_other, on=["Column2", "Column3"], how="inner")

# 'other' is an iterable with mixed types
df2 = DataFrame({"Column2": [10, 20, 30], "Column3": ["A", "B", "C"]})
mixed_iterable = [df2, 42]

with pytest.raises(TypeError, match="requires a pandas DataFrame or Series"):
df1.join(mixed_iterable, on=["Column2", "Column3"], how="inner")


class TestDataFrameJoin:
def test_join(self, multiindex_dataframe_random_data):
frame = multiindex_dataframe_random_data
Expand Down
Loading