AlenkaF opened a new pull request, #40803:
URL: https://github.com/apache/arrow/pull/40803

   ### Rationale for this change
   
   The conversion from `RecordBatch` to `Tensor` class exists but it doesn't 
support record batches with validity bitmaps. This PR adds support for an 
option to convert null values to NaN.
   
   ### What changes are included in this PR?
   
   This PR adds a `nul_to_nan` option in `RecordBatch::ToTensor` so that null 
values are converted to NaN in the resulting `Tensor`. This for example works:
   
   ```python
   >>> import pyarrow as pa
   >>> batch = pa.record_batch(
   ...     [
   ...         pa.array([1, 2, 3, 4, None], type=pa.int32()),
   ...         pa.array([10, 20, 30, 40, None], type=pa.float32()),
   ...     ], names = ["a", "b"]
   ... )
   
   >>> batch
   pyarrow.RecordBatch
   a: int32
   b: float
   ----
   a: [1,2,3,4,null]
   b: [10,20,30,40,null]
   
   >>> batch.to_tensor(null_to_nan=True)
   <pyarrow.Tensor>
   type: double
   shape: (5, 2)
   strides: (8, 40)
   
   >>> batch.to_tensor(null_to_nan=True).to_numpy()
   array([[ 1., 10.],
          [ 2., 20.],
          [ 3., 30.],
          [ 4., 40.],
          [nan, nan]])
   ```
   but default would raise:
   
   ```python
   >>> batch.to_tensor()
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "pyarrow/table.pxi", line 3421, in pyarrow.lib.RecordBatch.to_tensor
       a: int32
     File "pyarrow/error.pxi", line 154, in 
pyarrow.lib.pyarrow_internal_check_status
       return check_status(status)
     File "pyarrow/error.pxi", line 91, in pyarrow.lib.check_status
       raise convert_status(status)
   pyarrow.lib.ArrowTypeError: Can only convert a RecordBatch with no nulls. 
Set null_to_nan to true to convert nulls to nan
   ```
   
   ### Are these changes tested?
   
   Yes.
   
   ### Are there any user-facing changes?
   
   No.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to