jorisvandenbossche commented on code in PR #37533:
URL: https://github.com/apache/arrow/pull/37533#discussion_r1423709458


##########
python/pyarrow/array.pxi:
##########
@@ -3518,17 +3518,38 @@ 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 numpy.ndarray with 
zero copy.
+        First dimension of ndarray will be the length of the fixed shape 
tensor array
+        and the rest of the dimensions will match the shape of the fixed shape 
tensor.
+        """
+        return self.to_tensor().to_numpy()
+
+    def to_tensor(self):
+        cdef:
+            CFixedShapeTensorArray* ext_array = 
<CFixedShapeTensorArray*>(self.ap)
+            CResult[shared_ptr[CTensor]] ctensor
+        with nogil:
+            ctensor = ext_array.ToTensor()
+        return pyarrow_wrap_tensor(GetResultValue(ctensor))
 
-        Note: ``permutation`` should be trivial (``None`` or ``[0, 1, ..., 
len(shape)-1]``).
+    def get_tensor(self, int64_t i):
         """
-        if self.type.permutation is None or self.type.permutation == 
list(range(len(self.type.shape))):
-            np_flat = np.asarray(self.storage.flatten())
-            numpy_tensor = np_flat.reshape((len(self),) + 
tuple(self.type.shape))
-            return numpy_tensor
-        else:
-            raise ValueError(
-                'Only non-permuted tensors can be converted to numpy tensors.')
+        Get i-th tensor from fixed shape tensor extension array.
+
+        Parameters
+        ----------
+        i : int64_t

Review Comment:
   ```suggestion
           i : int
   ```



##########
python/pyarrow/array.pxi:
##########
@@ -3518,17 +3518,38 @@ 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 numpy.ndarray with 
zero copy.
+        First dimension of ndarray will be the length of the fixed shape 
tensor array
+        and the rest of the dimensions will match the shape of the fixed shape 
tensor.
+        """
+        return self.to_tensor().to_numpy()
+
+    def to_tensor(self):
+        cdef:
+            CFixedShapeTensorArray* ext_array = 
<CFixedShapeTensorArray*>(self.ap)
+            CResult[shared_ptr[CTensor]] ctensor
+        with nogil:
+            ctensor = ext_array.ToTensor()
+        return pyarrow_wrap_tensor(GetResultValue(ctensor))
 
-        Note: ``permutation`` should be trivial (``None`` or ``[0, 1, ..., 
len(shape)-1]``).
+    def get_tensor(self, int64_t i):
         """
-        if self.type.permutation is None or self.type.permutation == 
list(range(len(self.type.shape))):
-            np_flat = np.asarray(self.storage.flatten())
-            numpy_tensor = np_flat.reshape((len(self),) + 
tuple(self.type.shape))
-            return numpy_tensor
-        else:
-            raise ValueError(
-                'Only non-permuted tensors can be converted to numpy tensors.')
+        Get i-th tensor from fixed shape tensor extension array.
+
+        Parameters
+        ----------
+        i : int64_t
+            The index of the tensor to get.
+        Returns

Review Comment:
   ```suggestion
   
           Returns
   ```



-- 
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