From 2e2df005c73293d4db77474cc9cd81ca48fa3676 Mon Sep 17 00:00:00 2001 From: Dean MacGregor Date: Thu, 11 Apr 2024 13:41:23 -0400 Subject: [PATCH 1/5] fix(python): raise if DF is input to context --- py-polars/polars/_utils/parse_expr_input.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/py-polars/polars/_utils/parse_expr_input.py b/py-polars/polars/_utils/parse_expr_input.py index 1aa72d2b9226..a9ee8c80c794 100644 --- a/py-polars/polars/_utils/parse_expr_input.py +++ b/py-polars/polars/_utils/parse_expr_input.py @@ -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 @@ -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): + msg = "A DataFrame is an invalid input" + raise InvalidOperationError(msg) + elif isinstance(input, pl.LazyFrame): + msg = "A LazyFrame is an invalid input" + raise InvalidOperationError(msg) else: expr = F.lit(input, dtype=dtype) structify = False From fc8cf09b4aa8e82431ac443f364d4a658e28b421 Mon Sep 17 00:00:00 2001 From: Dean MacGregor Date: Thu, 11 Apr 2024 16:41:38 -0400 Subject: [PATCH 2/5] test --- py-polars/polars/_utils/parse_expr_input.py | 2 +- py-polars/tests/unit/utils/test_parse_expr_input.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/py-polars/polars/_utils/parse_expr_input.py b/py-polars/polars/_utils/parse_expr_input.py index a9ee8c80c794..a56a4c178385 100644 --- a/py-polars/polars/_utils/parse_expr_input.py +++ b/py-polars/polars/_utils/parse_expr_input.py @@ -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) ) diff --git a/py-polars/tests/unit/utils/test_parse_expr_input.py b/py-polars/tests/unit/utils/test_parse_expr_input.py index a17debfc94cb..2f9dd008d966 100644 --- a/py-polars/tests/unit/utils/test_parse_expr_input.py +++ b/py-polars/tests/unit/utils/test_parse_expr_input.py @@ -9,6 +9,7 @@ from polars._utils.parse_expr_input import parse_as_expression from polars._utils.wrap import wrap_expr from polars.testing import assert_frame_equal +from polars.exceptions import InvalidOperationError def assert_expr_equal(result: pl.Expr, expected: pl.Expr) -> None: @@ -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]) From 7cb56ed496367f80962ed5cf10923ee39451f10a Mon Sep 17 00:00:00 2001 From: Dean MacGregor Date: Thu, 11 Apr 2024 16:43:43 -0400 Subject: [PATCH 3/5] test imports organized --- py-polars/tests/unit/utils/test_parse_expr_input.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py-polars/tests/unit/utils/test_parse_expr_input.py b/py-polars/tests/unit/utils/test_parse_expr_input.py index 2f9dd008d966..c78182dad941 100644 --- a/py-polars/tests/unit/utils/test_parse_expr_input.py +++ b/py-polars/tests/unit/utils/test_parse_expr_input.py @@ -8,8 +8,8 @@ import polars as pl from polars._utils.parse_expr_input import parse_as_expression from polars._utils.wrap import wrap_expr -from polars.testing import assert_frame_equal from polars.exceptions import InvalidOperationError +from polars.testing import assert_frame_equal def assert_expr_equal(result: pl.Expr, expected: pl.Expr) -> None: From 0ef5850d8e8ad6132064e0657ca4649a9111c0d1 Mon Sep 17 00:00:00 2001 From: Dean MacGregor Date: Fri, 12 Apr 2024 07:11:09 -0400 Subject: [PATCH 4/5] move_errors_to_lit_and_lowercase --- py-polars/polars/_utils/parse_expr_input.py | 8 +------- py-polars/polars/functions/lit.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/py-polars/polars/_utils/parse_expr_input.py b/py-polars/polars/_utils/parse_expr_input.py index a56a4c178385..e09d2af93501 100644 --- a/py-polars/polars/_utils/parse_expr_input.py +++ b/py-polars/polars/_utils/parse_expr_input.py @@ -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, InvalidOperationError +from polars.exceptions import ComputeError with contextlib.suppress(ImportError): # Module not available when building docs import polars.polars as plr @@ -121,12 +121,6 @@ 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): - msg = "A DataFrame is an invalid input" - raise InvalidOperationError(msg) - elif isinstance(input, pl.LazyFrame): - msg = "A LazyFrame is an invalid input" - raise InvalidOperationError(msg) else: expr = F.lit(input, dtype=dtype) structify = False diff --git a/py-polars/polars/functions/lit.py b/py-polars/polars/functions/lit.py index b636d5f2544a..a2eaad095c50 100644 --- a/py-polars/polars/functions/lit.py +++ b/py-polars/polars/functions/lit.py @@ -15,6 +15,7 @@ from polars.datatypes import Date, Datetime, Duration, Time from polars.dependencies import _check_for_numpy from polars.dependencies import numpy as np +from polars.exceptions import InvalidOperationError with contextlib.suppress(ImportError): # Module not available when building docs import polars.polars as plr @@ -75,8 +76,13 @@ def lit( >>> pl.lit(pl.Series("y", [[1, 2], [3, 4]])) # doctest: +IGNORE_RESULT """ time_unit: TimeUnit - - if isinstance(value, datetime): + if isinstance(input, pl.DataFrame): + msg = "a DataFrame is an invalid input" + raise InvalidOperationError(msg) + elif isinstance(input, pl.LazyFrame): + msg = "a LazyFrame is an invalid input" + raise InvalidOperationError(msg) + elif isinstance(value, datetime): if dtype is not None and (tu := getattr(dtype, "time_unit", "us")) is not None: time_unit = tu # type: ignore[assignment] else: From c42184c4a42714312e40e64250aba4a62a62247a Mon Sep 17 00:00:00 2001 From: Dean MacGregor Date: Fri, 12 Apr 2024 07:59:26 -0400 Subject: [PATCH 5/5] param_name --- py-polars/polars/functions/lit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py-polars/polars/functions/lit.py b/py-polars/polars/functions/lit.py index a2eaad095c50..43327d69e7b8 100644 --- a/py-polars/polars/functions/lit.py +++ b/py-polars/polars/functions/lit.py @@ -76,10 +76,10 @@ def lit( >>> pl.lit(pl.Series("y", [[1, 2], [3, 4]])) # doctest: +IGNORE_RESULT """ time_unit: TimeUnit - if isinstance(input, pl.DataFrame): + if isinstance(value, pl.DataFrame): msg = "a DataFrame is an invalid input" raise InvalidOperationError(msg) - elif isinstance(input, pl.LazyFrame): + elif isinstance(value, pl.LazyFrame): msg = "a LazyFrame is an invalid input" raise InvalidOperationError(msg) elif isinstance(value, datetime):