pitrou commented on code in PR #37533:
URL: https://github.com/apache/arrow/pull/37533#discussion_r1482681845
##########
python/pyarrow/array.pxi:
##########
@@ -3570,27 +3570,57 @@ class FixedShapeTensorArray(ExtensionArray):
def to_numpy_ndarray(self):
"""
- Convert fixed shape tensor extension array to a numpy array (with
dim+1).
+ Convert fixed shape tensor extension array to a multi-dimensional
numpy.ndarray.
- Note: ``permutation`` should be trivial (``None`` or ``[0, 1, ...,
len(shape)-1]``).
+ The resulting ndarray will have (ndim + 1) dimensions.
+ The size of the first dimension will be the length of the fixed shape
tensor array
+ and the rest of the dimensions will match the permuted shape of the
fixed
+ shape tensor.
+
+ The conversion is zero-copy.
+
Review Comment:
Nit: remove extraneous empty line.
```suggestion
```
##########
python/pyarrow/array.pxi:
##########
@@ -3573,17 +3602,19 @@ class FixedShapeTensorArray(ExtensionArray):
]
]
"""
- if not obj.flags["C_CONTIGUOUS"]:
- raise ValueError('The data in the numpy array need to be in a
single, '
- 'C-style contiguous segment.')
+
+ permutation = (-np.array(obj.strides)).argsort(kind='stable')
+ if permutation[0] != 0:
+ raise ValueError('First stride needs to be largest to ensure that '
+ 'individual tensor data is contiguous in memory.')
arrow_type = from_numpy_dtype(obj.dtype)
- shape = obj.shape[1:]
- size = obj.size / obj.shape[0]
+ shape = np.take(obj.shape, permutation)
+ values = np.ravel(obj, order="K")
Review Comment:
`as_strided` can be a later PR if desired. The docstring addition is good
for now!
--
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]