If precision is not important you can cast the column to float64 first.
>>> x = pa.array([1, 2, 3], type=pa.decimal128(6, 1))
>>> x.cast(pa.float64()).cast(pa.string())
<pyarrow.lib.StringArray object at 0x7fd23a52cd00>
[
"1",
"2",
"3"
]
If precision is important you could use python or pandas to do the
conversion to string.
>>> pa.array([str(v) for v in x.to_pylist()])
<pyarrow.lib.StringArray object at 0x7fd23a52cd00>
[
"1.0",
"2.0",
"3.0"
]
On Wed, Jul 6, 2022 at 5:45 AM François Pacull
<[email protected]> wrote:
>
> Dear Arrow team and users, I have a simple question regarding the decimal
> data type with pyarrow. I am trying to cast a table with decimal columns to
> string, or to write it to a csv file. In both cases I get the error message:
>
> pyarrow.lib.ArrowNotImplementedError: Unsupported cast from
> decimal128(18, 9) to utf8 using function cast_string
>
> I understand that is not implemented yet, but is there by chance a way to get
> around this?
> Thanks, François.
>
> PS: I am using Python : 3.9.13 & pyarrow : 8.0.0
> Here is a code snippet:
>
> import decimal
>
> import pyarrow as pa
> import pyarrow.compute as pc
> import pyarrow.csv
>
> PREC, SCAL = 18, 9 # decimal precision & scale
>
> context = decimal.getcontext()
> context.prec = PREC
> ref_decimal = decimal.Decimal('0.123456789')
>
> float_numbers = [0.1, 654.5, 4.65742]
> decimal_numbers = [
> decimal.Decimal(str(f)).quantize(ref_decimal) for f in float_numbers
> ]
>
> pa_arr_dec = pa.array(
> decimal_numbers, type=pa.decimal128(precision=PREC, scale=SCAL)
> )
> pa_arr_str = pc.cast(pa_arr_dec, pa.string())
>
>
> Traceback (most recent call last):
> File "/home/francois/Workspace/.../scripts/pyarrow_decimal.py", line 21,
> in <module>
> pa_arr_str = pc.cast(pa_arr_dec, pa.string())
> File
> "/home/francois/miniconda3/envs/tableau2/lib/python3.9/site-packages/pyarrow/compute.py",
> line 376, in cast
> return call_function("cast", [arr], options)
> File "pyarrow/_compute.pyx", line 542, in pyarrow._compute.call_function
> File "pyarrow/_compute.pyx", line 341, in pyarrow._compute.Function.call
> File "pyarrow/error.pxi", line 144, in
> pyarrow.lib.pyarrow_internal_check_status
> File "pyarrow/error.pxi", line 121, in pyarrow.lib.check_status
> pyarrow.lib.ArrowNotImplementedError: Unsupported cast from decimal128(18,
> 9) to utf8 using function cast_string