rok commented on code in PR #37533:
URL: https://github.com/apache/arrow/pull/37533#discussion_r1425374789
##########
python/pyarrow/array.pxi:
##########
@@ -3573,17 +3595,20 @@ 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()
+ if not 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.lib.stride_tricks.as_strided(
+ obj, shape=(obj.size,), strides=(obj.dtype.itemsize,))
Review Comment:
The reason for the change was that ravel states in the doc: `[..] A copy is
made only if needed. [..]` while `as_strided` only creates a view. I would
prefer `ravel` as it's less error prone but I can't immediately see from
[source](https://github.com/numpy/numpy/blob/v1.26.0/numpy/core/fromnumeric.py#L1768-L1874)
when it would incur a copy.
--
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]