Skip to content

[Python] support conversion to decimal type from floats? #16805

@asfimport

Description

@asfimport

We currently allow constructing a decimal array from decimal.Decimal objects or from ints:

In [14]: pa.array([1, 0], type=pa.decimal128(2))                                                                                                              
Out[14]: 
<pyarrow.lib.Decimal128Array object at 0x7f51fa2da818>
[
  1,
  0
]

In [31]: pa.array([decimal.Decimal('0.1'), decimal.Decimal('0.2')], pa.decimal128(2, 1))                                                                      
Out[31]: 
<pyarrow.lib.Decimal128Array object at 0x7fce671172b0>
[
  0.1,
  0.2
]

but not from floats (or strings):

In [18]: pa.array([0.1, 0.2], pa.decimal128(2))                                                                                                               
...
ArrowTypeError: int or Decimal object expected, got float

Is this something we would like to support?

There are for sure precision issues you run into, but if the decimal type is fully specified, it seems clear what the user wants. In general, since decimal objects in pandas are not that easy to work with, many people might have plain float columns that they want to convert to decimal.

Reporter: Joris Van den Bossche / @jorisvandenbossche

Related issues:

Note: This issue was originally created as ARROW-5905. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions