AlenkaF opened a new pull request, #40867:
URL: https://github.com/apache/arrow/pull/40867
### Rationale for this change
The conversion from `RecordBatch` to `Tensor` class now exists but it
doesn't support row-major `Tensor` as an output. This PR adds support for an
option to construct row-major `Tensor`.
### What changes are included in this PR?
This PR adds a `row_major` option in `RecordBatch::ToTensor` so that
row-major `Tensor` can be constructed. The default conversion will be
row-major. This for example works:
```python
>>> import pyarrow as pa
>>> import numpy as np
>>> arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> arr2 = [10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> batch = pa.RecordBatch.from_arrays(
... [
... pa.array(arr1, type=pa.uint16()),
... pa.array(arr2, type=pa.int16()),
...
... ], ["a", "b"]
... )
# Row-major
>>> batch.to_tensor()
<pyarrow.Tensor>
type: int32
shape: (9, 2)
strides: (8, 4)
>>> batch.to_tensor().to_numpy().flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
# Column-major
>>> batch.to_tensor(row_major=False)
<pyarrow.Tensor>
type: int32
shape: (9, 2)
strides: (4, 36)
>>> batch.to_tensor(row_major=False).to_numpy().flags
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
```
### Are these changes tested?
Yes, in C++ and Python.
### 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]