polars icon indicating copy to clipboard operation
polars copied to clipboard

Initialize `Series` of `dtype=pl.Decimal` directly from list of python `int`s

Open sslivkoff opened this issue 1 year ago • 3 comments

Problem description

It would be nice to be able to do this:

numbers = [2963091539321097135000000000, 25658709114149718824803874]
pl.Series(numbers, dtype=pl.Decimal)

But doing so raises ComputeError: unable to convert any-value of dtype f64 to decimal.

So instead it is necessary to manually convert to decimal first. Running pl.Config.activate_decimals() does not remove the error

sslivkoff avatar May 28 '23 23:05 sslivkoff

It looks an error only happens when using large ints. This works without error:

numbers = [1, 2]
pl.Series(numbers, dtype=pl.Decimal)

sslivkoff avatar May 28 '23 23:05 sslivkoff

This throws an error as Python has large integer support, but to interop with Rust it has to convert values > Int64 to Float64, which almost always guarantees a loss of precision - at which point you don't have the value you thought you had. I think it's reasonable to expect decimals as input here as the error prevents a worse scenario ;)

alexander-beedie avatar Jan 15 '24 19:01 alexander-beedie

(Actually, I'll keep it open for now in case we can leverage i128 conversions in the future 🤔)

alexander-beedie avatar Jan 15 '24 19:01 alexander-beedie