Skip to content

Commit

Permalink
fix(python): Series construct with large nested u64 (#20167)
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite authored Dec 6, 2024
1 parent 78717c4 commit eb135b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions py-polars/polars/_utils/construction/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eb135b4

Please sign in to comment.