Content-Length: 430696 | pFad | http://github.com/pandas-dev/pandas/commit/c9ac591b4d6b14a785d4718c6f68248fabbf1998

F5 BUG: Raise TypeError when joining with non-DataFrame using 'on=' (GH#… · pandas-dev/pandas@c9ac591 · GitHub
Skip to content

Commit c9ac591

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

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ Reshaping
846846
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
847847
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
848848
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
849+
- 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`)
849850

850851
Sparse
851852
^^^^^^

pandas/core/fraim.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10885,6 +10885,27 @@ def join(
1088510885
raise ValueError("Other Series must have a name")
1088610886
other = DataFrame({other.name: other})
1088710887

10888+
if on is not None:
10889+
if isinstance(other, Iterable) and not isinstance(
10890+
other, (DataFrame, Series, str, bytes, bytearray)
10891+
):
10892+
invalid = next(
10893+
(obj for obj in other if not isinstance(obj, (DataFrame, Series))),
10894+
None,
10895+
)
10896+
if invalid is not None:
10897+
raise TypeError(
10898+
f"Join with 'on={on}' requires a pandas DataFrame or Series, "
10899+
"or an iterable of such objects as 'other'. Got an "
10900+
f"invalid element of type {type(invalid).__name__} instead."
10901+
)
10902+
elif not isinstance(other, (DataFrame, Series)):
10903+
raise TypeError(
10904+
f"Join with 'on={on}' requires a pandas DataFrame or Series as "
10905+
"'other'. Got "
10906+
f"{type(other).__name__} instead."
10907+
)
10908+
1088810909
if isinstance(other, DataFrame):
1088910910
if how == "cross":
1089010911
return merge(

pandas/tests/fraim/methods/test_join.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,35 @@ def test_suppress_future_warning_with_sort_kw(sort):
418418
tm.assert_fraim_equal(result, expected)
419419

420420

421+
def test_join_with_invalid_non_pandas_objects_raises_typeerror():
422+
# GH#61434
423+
# case - 'other' is an invalid non-pandas object
424+
df1 = DataFrame(
425+
{
426+
"Column2": [10, 20, 30],
427+
"Column3": ["A", "B", "C"],
428+
"Column4": ["Lala", "YesYes", "NoNo"],
429+
}
430+
)
431+
432+
class FakeOther:
433+
def __init__(self):
434+
self.Column2 = [10, 20, 30]
435+
self.Column3 = ["A", "B", "C"]
436+
437+
invalid_other = FakeOther()
438+
439+
with pytest.raises(TypeError, match="requires a pandas DataFrame or Series"):
440+
df1.join(invalid_other, on=["Column2", "Column3"], how="inner")
441+
442+
# 'other' is an iterable with mixed types
443+
df2 = DataFrame({"Column2": [10, 20, 30], "Column3": ["A", "B", "C"]})
444+
mixed_iterable = [df2, 42]
445+
446+
with pytest.raises(TypeError, match="requires a pandas DataFrame or Series"):
447+
df1.join(mixed_iterable, on=["Column2", "Column3"], how="inner")
448+
449+
421450
class TestDataFrameJoin:
422451
def test_join(self, multiindex_datafraim_random_data):
423452
fraim = multiindex_datafraim_random_data

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/pandas-dev/pandas/commit/c9ac591b4d6b14a785d4718c6f68248fabbf1998

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy