Skip to content
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

fix(python): raise if DF is input to context (with_columns, select, etc) #15598

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions py-polars/polars/_utils/parse_expr_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import polars._reexport as pl
from polars import functions as F
from polars._utils.deprecation import issue_deprecation_warning
from polars.exceptions import ComputeError
from polars.exceptions import ComputeError, InvalidOperationError

with contextlib.suppress(ImportError): # Module not available when building docs
import polars.polars as plr
Expand Down Expand Up @@ -71,7 +71,7 @@ def _parse_inputs_as_iterable(

def _is_iterable(input: Any | Iterable[Any]) -> bool:
return isinstance(input, Iterable) and not isinstance(
input, (str, bytes, pl.Series)
input, (str, bytes, pl.Series, pl.DataFrame, pl.LazyFrame)
)


Expand Down Expand Up @@ -121,6 +121,12 @@ def parse_as_expression(
elif isinstance(input, list) and not list_as_lit:
expr = F.lit(pl.Series(input), dtype=dtype)
structify = False
elif isinstance(input, pl.DataFrame):
deanm0000 marked this conversation as resolved.
Show resolved Hide resolved
msg = "A DataFrame is an invalid input"
deanm0000 marked this conversation as resolved.
Show resolved Hide resolved
raise InvalidOperationError(msg)
elif isinstance(input, pl.LazyFrame):
msg = "A LazyFrame is an invalid input"
deanm0000 marked this conversation as resolved.
Show resolved Hide resolved
raise InvalidOperationError(msg)
else:
expr = F.lit(input, dtype=dtype)
structify = False
Expand Down
11 changes: 11 additions & 0 deletions py-polars/tests/unit/utils/test_parse_expr_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import polars as pl
from polars._utils.parse_expr_input import parse_as_expression
from polars._utils.wrap import wrap_expr
from polars.exceptions import InvalidOperationError
from polars.testing import assert_frame_equal


Expand Down Expand Up @@ -78,3 +79,13 @@ def test_parse_as_expression_structify_multiple_outputs() -> None:
result = wrap_expr(parse_as_expression(pl.col("*"), structify=True))
expected = pl.struct("a", "b")
assert_expr_equal(result, expected)


def test_raise_with_df_in_context() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})
df2 = pl.DataFrame({"b": [2, 3, 4]})
with pytest.raises(InvalidOperationError):
df.with_columns(df2)

with pytest.raises(InvalidOperationError):
df.select([df2])
Loading