-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
cast
of float to pl.Decimal
silently fails but also changes float values
#12775
Comments
There is a combination of two problems here:
In [5]: pl.DataFrame({"x": [1.5]}).with_columns(
...: x2=pl.col("x").cast(pl.Decimal(scale=4)),
...: x3=pl.col("x").cast(pl.Utf8).cast(pl.Decimal(scale=4)),
...: )
Out[5]:
shape: (1, 3)
┌─────┬───────────────┬──────────────┐
│ x ┆ x2 ┆ x3 │
│ --- ┆ --- ┆ --- │
│ f64 ┆ decimal[38,4] ┆ decimal[*,4] │
╞═════╪═══════════════╪══════════════╡
│ 1.5 ┆ 1.5000 ┆ 1.5000 │
└─────┴───────────────┴──────────────┘ Scale inference for floats would likely be problematic or infeasible: after all, if there was a perfect way to encode decimal numbers in floats, there wouldn't be much of a need for a decimal type! I think the most reasonable option here would be to not allow casts from floats to decimals without a specified scale, and truncate or round at that scale. This would not be consistent with string to decimal casts (infer scale if not specified, fail if number can't be exactly represented at a specified scale), but would make more sense for floats, and would make reliable casts viable in many circumstances. The problem with doing this, however, is that with the current arrangement, there doesn't appear to be a way to distinguish between |
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
Log output
No response
Issue description
casting a float to a decimal silently fails and does NOT change the type but somehow changes the data (1.5 -> 1.0)
Expected behavior
option 1: it should just work
option 2: show error that only string can be converted to decimal and tell user to cast to uft8 first
Installed versions
The text was updated successfully, but these errors were encountered: