Skip to content

Commit

Permalink
python: improve error when passing DataFrame to LazyFrame (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 23, 2022
1 parent d64eb0b commit 94b40f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,8 @@ def join_asof(
force_parallel
Force the physical plan to evaluate the computation of both DataFrames up to the join in parallel.
"""
if not isinstance(ldf, LazyFrame):
raise ValueError(f"Expected a `LazyFrame` as join table, got {type(ldf)}")

if isinstance(on, str):
left_on = on
Expand Down Expand Up @@ -1296,6 +1298,9 @@ def join(
└──────┴──────┴─────┴───────┘
"""
if not isinstance(ldf, LazyFrame):
raise ValueError(f"Expected a `LazyFrame` as join table, got {type(ldf)}")

if how == "asof":
warnings.warn(
"using asof join via LazyFrame.join is deprecated, please use LazyFrame.join_asof",
Expand Down
24 changes: 24 additions & 0 deletions py-polars/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import typing

import numpy as np
import pytest
Expand Down Expand Up @@ -57,3 +58,26 @@ def test_panic_exception() -> None:
match=r"""this operation is not implemented/valid for this dtype: .*""",
):
pl.struct(pl.Series("a", [1, 2, 3]), eager=True).sort()


@typing.no_type_check
def test_join_lazy_on_df() -> None:
df_left = pl.DataFrame(
{
"Id": [1, 2, 3, 4],
"Names": ["A", "B", "C", "D"],
}
)
df_right = pl.DataFrame({"Id": [1, 3], "Tags": ["xxx", "yyy"]})

with pytest.raises(
ValueError,
match="Expected a `LazyFrame` as join table, got <class 'polars.internals.frame.DataFrame'>",
):
df_left.lazy().join(df_right, on="Id")

with pytest.raises(
ValueError,
match="Expected a `LazyFrame` as join table, got <class 'polars.internals.frame.DataFrame'>",
):
df_left.lazy().join_asof(df_right, on="Id")

0 comments on commit 94b40f2

Please sign in to comment.