polars
polars copied to clipboard
"Incorrect" / No rounding when casting `str` to `Decimal`
Description
casting from str
to Decimal
truncates the data without rounding.
Other libraries round here.
df = pl.DataFrame({"x": ["0.04", "0.05", "0.06"]}).with_columns(
d=pl.col("x").cast(pl.Decimal(scale=1)),
)
# shape: (3, 2)
# ┌──────┬──────────────┐
# │ x ┆ d │
# │ --- ┆ --- │
# │ str ┆ decimal[*,1] │
# ╞══════╪══════════════╡
# │ 0.04 ┆ 0 │
# │ 0.05 ┆ 0 │
# │ 0.06 ┆ 0 │
# └──────┴──────────────┘
duckdb.sql("""
select
x,
x::decimal(18,1) as d,
from
df
""")
# ┌─────────┬───────────────┐
# │ x │ d │
# │ varchar │ decimal(18,1) │
# ├─────────┼───────────────┤
# │ 0.04 │ 0.0 │
# │ 0.05 │ 0.1 │
# │ 0.06 │ 0.1 │
# └─────────┴───────────────┘