pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
26.63k stars 1.63k forks source link

Off-by-one error when casting to Decimal with set precision #16027

Closed stinodego closed 1 week ago

stinodego commented 2 weeks ago

Checks

Reproducible example

import polars as pl

v = Decimal("99999.99999999999999999999")
s = pl.Series([v])
result = s.cast(pl.Decimal(25, 20))
print(result)

Log output

Traceback (most recent call last):
  File "/home/stijn/code/polars/py-polars/repro.py", line 22, in <module>
    s = pl.Series([v], dtype=pl.Decimal(25, 20))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stijn/code/polars/py-polars/polars/series/series.py", line 312, in __init__
    self._s = sequence_to_pyseries(
              ^^^^^^^^^^^^^^^^^^^^^
  File "/home/stijn/code/polars/py-polars/polars/_utils/construction/series.py", line 152, in sequence_to_pyseries
    pyseries = pyseries.cast(dtype, strict=strict)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.ComputeError: conversion from `decimal[*,20]` to `decimal[25,20]` failed in column '' for 1 out of 1 values: [99999.99999999999999999999]

Issue description

The decimal number has 25 places (5 before / 20 after the decimal point). Casting to precision 25 should work. Casting to precision 26 does work, however.

This issue does not occur with lower precision numbers:

import polars as pl

v = Decimal("99999.99999")
s = pl.Series([v], dtype=pl.Decimal(10, 5))
print(s)  # works

Expected behavior

No error.

Installed versions

main