You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "AlenkaF (via GitHub)" <gi...@apache.org> on 2024/04/02 10:41:07 UTC

Re: [I] [Python] mix of np.float16/np.float128 and integers converts to Int64Array, not DoubleArray [arrow]

AlenkaF commented on issue #40910:
URL: https://github.com/apache/arrow/issues/40910#issuecomment-2031662863

   If I understand correctly this is the downside of `pa.array` constructor when sequence has mixed NumPy scalars and other kinds of Python scalars. The handling of different types of scalars in sequence is found here: https://github.com/apache/arrow/blob/42b49df0f3dc1586ad38c608ec93f382a4f4e3c4/python/pyarrow/src/arrow/python/inference.cc#L474-L503
   
   where first the sequence is checked for all different types and then the uniform type is selected. As you can see only `float32` and `float64` are supported in the conversion and my guess is that it is defaulted to a `uint` type or similar. Not sure where in the code this happens though.
   
   What I would suggest is to use `type` keyword in the `pa.array` constructor:
   
   ```python
   >>> pa.array([np.float16(0.5), 1], type=pa.float64())
   <pyarrow.lib.DoubleArray object at 0x127e5f1c0>
   [
     0.5,
     1
   ]
   >>> pa.array([np.float16(0.5), 1], type=pa.float32())
   <pyarrow.lib.FloatArray object at 0x103138460>
   [
     0.5,
     1
   ]
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org