diff --git a/py-polars/polars/_utils/construction/series.py b/py-polars/polars/_utils/construction/series.py index 0e582d429014..66ace911fef1 100644 --- a/py-polars/polars/_utils/construction/series.py +++ b/py-polars/polars/_utils/construction/series.py @@ -310,8 +310,18 @@ def _construct_series_with_fallbacks( """Construct Series, with fallbacks for basic type mismatch (eg: bool/int).""" try: return constructor(name, values, strict) - except TypeError: - if dtype is None: + except (TypeError, OverflowError) as e: + # # This retry with i64 is related to https://github.com/pola-rs/polars/issues/17231 + # # Essentially, when given a [0, u64::MAX] then it would Overflow. + if ( + isinstance(e, OverflowError) + and dtype is None + and constructor == PySeries.new_opt_i64 + ): + return _construct_series_with_fallbacks( + PySeries.new_opt_u64, name, values, dtype, strict=strict + ) + elif dtype is None: return PySeries.new_from_any_values(name, values, strict=strict) else: return PySeries.new_from_any_values_and_dtype( diff --git a/py-polars/tests/unit/series/test_series.py b/py-polars/tests/unit/series/test_series.py index 0ed478b9aa83..b68427b1a471 100644 --- a/py-polars/tests/unit/series/test_series.py +++ b/py-polars/tests/unit/series/test_series.py @@ -2172,3 +2172,12 @@ def test_series_from_numpy_with_dtype() -> None: def test_raise_invalid_is_between() -> None: with pytest.raises(pl.exceptions.InvalidOperationError): pl.select(pl.lit(2).is_between(pl.lit("11"), pl.lit("33"))) + + +def test_construction_large_nested_u64_17231() -> None: + import polars as pl + + values = [{"f0": [9223372036854775808]}] + dtype = pl.Struct({"f0": pl.List(pl.UInt64)}) + + assert pl.Series(values, dtype=dtype).to_list() == values