Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
stinodego opened this issue May 2, 2024 · 0 comments · Fixed by #16049
Closed
2 tasks done

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

stinodego opened this issue May 2, 2024 · 0 comments · Fixed by #16049
Assignees
Labels
A-dtype-decimal Area: decimal data type accepted Ready for implementation bug Something isn't working P-low Priority: low python Related to Python Polars

Comments

@stinodego
Copy link
Member

stinodego commented May 2, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

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

@stinodego stinodego added bug Something isn't working python Related to Python Polars needs triage Awaiting prioritization by a maintainer A-dtype-decimal Area: decimal data type P-low Priority: low and removed needs triage Awaiting prioritization by a maintainer labels May 2, 2024
@stinodego stinodego self-assigned this May 4, 2024
@c-peters c-peters added the accepted Ready for implementation label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dtype-decimal Area: decimal data type accepted Ready for implementation bug Something isn't working P-low Priority: low python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants