diff --git a/py-polars/polars/datatypes.py b/py-polars/polars/datatypes.py index e40adfabfc24..977e40bc2339 100644 --- a/py-polars/polars/datatypes.py +++ b/py-polars/polars/datatypes.py @@ -801,5 +801,10 @@ def maybe_cast( py_type = dtype_to_py_type(dtype) if not isinstance(el, py_type): - el = py_type(el) # type: ignore[call-arg] + try: + el = py_type(el) # type: ignore[call-arg] + except Exception: + raise ValueError( + f"Cannot convert Python type {type(el)} to {dtype}" + ) from None return el diff --git a/py-polars/tests/unit/test_series.py b/py-polars/tests/unit/test_series.py index 36e2ada41ea9..84a511bf90ab 100644 --- a/py-polars/tests/unit/test_series.py +++ b/py-polars/tests/unit/test_series.py @@ -340,7 +340,7 @@ def test_arithmetic(s: pl.Series) -> None: 2 % a with pytest.raises(ValueError): 2**a - with pytest.raises(TypeError): # https://github.com/pola-rs/polars/issues/6617 + with pytest.raises(ValueError): +a a = pl.Series("a", [""]) with pytest.raises(ValueError):