polars
polars copied to clipboard
`float` -> `Decimal` truncates Data by default (`str` -> `Decimal` does NOT)
Description
When casting from float
or str
to Decimal
without scale
I see two different behaviours
-
float
->Decimal
: defaults toprecision=38
&scale=0
-
str
->Decimal
: defaults toprecision=*
andscale
is determined by the data
Not sure about the "best" solution but might be good to make this consistent. I see 2 options
- determine
scale
by looking at the data - use sensible defalut like
scale=0
and make user explicitly specify the desiredscale
.
import polars as pl
pl.Config.activate_decimals(active=True)
pl.DataFrame({"f": [1.23], "s": ["1.23"]}).with_columns(
float=pl.col("f").cast(pl.Decimal),
str=pl.col("s").cast(pl.Decimal),
)
# shape: (1, 4)
# ┌──────┬──────┬───────────────┬──────────────┐
# │ f ┆ s ┆ float ┆ str │
# │ --- ┆ --- ┆ --- ┆ --- │
# │ f64 ┆ str ┆ decimal[38,0] ┆ decimal[*,2] │
# ╞══════╪══════╪═══════════════╪══════════════╡
# │ 1.23 ┆ 1.23 ┆ 1 ┆ 1.23 │
# └──────┴──────┴───────────────┴──────────────┘