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: