From 96490f22f102b420f5f47befed89d90113631c62 Mon Sep 17 00:00:00 2001 From: J van Zundert Date: Sun, 5 Feb 2023 07:13:46 +0000 Subject: [PATCH] fix(python): Raise ValueError on adding float to Series of dtype date (#6677) --- py-polars/polars/datatypes.py | 7 ++++++- py-polars/tests/unit/test_series.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) 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):