apache / apache/arrow

[Python] Cannot directly create scalars or arrays of type `pa.decimal128`

Open
#38,019 1 comment 0 reactions 0 assignees View on GitHub
Component: Python Type: bug
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

Trying to create a scalar or array of type `pa.decimal128(4, 2)` fails:

```python
In [90]: pa.scalar(1.2, type=pa.decimal128(4, 2))
---------------------------------------------------------------------------
ArrowTypeError Traceback (most recent call last)
Cell In[90], line 1
----> 1 pa.scalar(1.2, type=pa.decimal128(4, 2))

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\scalar.pxi:1122, in pyarrow.lib.scalar()

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\error.pxi:144, in pyarrow.lib.pyarrow_internal_check_status()

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\error.pxi:123, in pyarrow.lib.check_status()

ArrowTypeError: int or Decimal object expected, got float

In [91]: pa.array([1.2, 3.4], type=pa.decimal128(4, 2))
---------------------------------------------------------------------------
ArrowTypeError Traceback (most recent call last)
Cell In[91], line 1
----> 1 pa.array([1.2, 3.4], type=pa.decimal128(4, 2))

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\array.pxi:327, in pyarrow.lib.array()

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\array.pxi:39, in pyarrow.lib._sequence_to_array()

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\error.pxi:144, in pyarrow.lib.pyarrow_internal_check_status()

File ~\projects\ML_App_Template\venv\Lib\site-packages\pyarrow\error.pxi:123, in pyarrow.lib.check_status()

ArrowTypeError: int or Decimal object expected, got float
```

But creating a float array and then casting to `pa.decimal128` does work:

```python
In [92]: pa.scalar(1.2).cast(pa.decimal128(4, 2))
Out[92]:

In [93]: pa.array([1.2, 3.4]).cast(pa.decimal128(4, 2))
Out[93]:

[
1.20,
3.40
]
```

Can the direct construction (without `cast`) be made to work? I couldn't find any documentation on other ways to create a decimal, like `Decimal('1.20')`. I've seen `Decimal('1.20')` used in a different github issue, but I can't figure out which module `Decimal` is in (if it still exists).

```python
In [94]: pa.__version__
Out[94]: '13.0.0'
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.