Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
iabhi4 wants to merge1 commit intopandas-dev:main
base:main
Choose a base branch
Loading
fromiabhi4:fix-61434-nonpandas-join-typeerror
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsdoc/source/whatsnew/v3.0.0.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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
21 changes: 21 additions & 0 deletionspandas/core/frame.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10885,6 +10885,27 @@ 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
ifisinstance(other,Iterable)andnotisinstance(
other, (DataFrame,Series,str,bytes,bytearray)
):
ifis_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 inmerge

Copy link
Member

@rhshadrachrhshadrachMay 19, 2025
edited
Loading

Choose a reason for hiding this comment

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

@mroeschke - is_list_like will return True on a DataFrame. We only want to enter this block on a potential sequence of DataFrame/Series.

Copy link
Member

Choose a reason for hiding this comment

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

Ah OK ignore my suggestion then. But I believe this check should still be done inmerge probably

rhshadrach and iabhi4 reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Ah OK ignore my suggestion then. But I believe this check should still be done inmerge probably

ValueError is raised injoin() before the call reachesmerge whenon is specified. Would you prefer that I let these inputs flow intomerge and move the check there for consistency?

invalid = next(
(obj for obj in other if not isinstance(obj, (DataFrame, Series))),
None,
)
if invalid is not None:
raise TypeError(
f"Join with 'on={on}' requires a pandas DataFrame or Series, "
"or an iterable of such objects as 'other'. Got an "
f"invalid element of type {type(invalid).__name__} instead."
)
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 deletionspandas/tests/frame/methods/test_join.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp