diff --git a/py-polars/src/functions/lazy.rs b/py-polars/src/functions/lazy.rs index 704658f9d8ccf..06b0bbe12e56a 100644 --- a/py-polars/src/functions/lazy.rs +++ b/py-polars/src/functions/lazy.rs @@ -451,7 +451,11 @@ pub fn repeat(value: PyExpr, n: PyExpr, dtype: Option>) -> PyResu } if let Expr::Literal(lv) = &value { - let av = lv.to_any_value().unwrap(); + let av = lv.to_any_value().ok_or_else(||{ + PyPolarsErr::from(polars_err!(ComputeError: + "The data type of the resulting column can't be inferred from the given value, please explicitly specify the dtype." + )) + })?; // Integer inputs that fit in Int32 are parsed as such if let DataType::Int64 = av.dtype() { let int_value = av.try_extract::().unwrap(); diff --git a/py-polars/tests/unit/functions/test_repeat.py b/py-polars/tests/unit/functions/test_repeat.py index 4b1d3138b592a..afc772149b989 100644 --- a/py-polars/tests/unit/functions/test_repeat.py +++ b/py-polars/tests/unit/functions/test_repeat.py @@ -6,6 +6,7 @@ import pytest import polars as pl +from polars import ComputeError from polars.testing import assert_frame_equal, assert_series_equal @@ -201,3 +202,8 @@ def test_repeat_by_none_13053(data: list[Any], expected_data: list[list[Any]]) - res = df.select(repeat=pl.col("x").repeat_by("by")) expected = pl.Series("repeat", expected_data) assert_series_equal(res.to_series(), expected) + + +def test_repeat_raise() -> None: + with pytest.raises(ComputeError, match="please explicitly specify the dtype"): + pl.repeat([1, 2], n=2)