AlenkaF commented on code in PR #50203:
URL: https://github.com/apache/arrow/pull/50203#discussion_r3467238608


##########
python/pyarrow/src/arrow/python/python_to_arrow.cc:
##########
@@ -908,13 +908,32 @@ class PyListConverter : public ListConverter<T, 
PyConverter, PyConverterTrait> {
 
   Status AppendNdarray(PyObject* value) {
     PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(value);
-    if (PyArray_NDIM(ndarray) != 1) {
-      return Status::Invalid("Can only convert 1-dimensional array values");
-    }
     if (PyArray_ISBYTESWAPPED(ndarray)) {
       // TODO
       return Status::NotImplemented("Byte-swapped arrays not supported");
     }
+    OwnedRef flattened;
+    if (PyArray_NDIM(ndarray) != 1) {
+      // GH-49644: a fixed-size list (e.g. fixed-shape-tensor storage) can be
+      // built from a multi-dimensional array, always flattened in C order
+      // regardless of the input's memory layout.
+      if (PyArray_NDIM(ndarray) < 2 || this->list_type_->id() != 
Type::FIXED_SIZE_LIST) {
+        return Status::Invalid(
+            "Can only convert 1-dimensional array values to a variable-sized 
list");
+      }
+      // Get an aligned, C-contiguous array (copying only if needed), then view
+      // it as 1-D so its values can be read directly in C order.
+      PyObject* contiguous =
+          PyArray_CheckFromAny(value, nullptr, /*min_depth=*/0, 
/*max_depth=*/0,
+                               NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED, 
nullptr);
+      RETURN_IF_PYERROR();
+      flattened.reset(
+          PyArray_Ravel(reinterpret_cast<PyArrayObject*>(contiguous), 
NPY_CORDER));

Review Comment:
   I think you can directly use `PyArray_DATA` on the `PyObject* contiguous`.



##########
python/pyarrow/src/arrow/python/python_to_arrow.cc:
##########
@@ -908,13 +908,32 @@ class PyListConverter : public ListConverter<T, 
PyConverter, PyConverterTrait> {
 
   Status AppendNdarray(PyObject* value) {
     PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(value);
-    if (PyArray_NDIM(ndarray) != 1) {
-      return Status::Invalid("Can only convert 1-dimensional array values");
-    }
     if (PyArray_ISBYTESWAPPED(ndarray)) {
       // TODO
       return Status::NotImplemented("Byte-swapped arrays not supported");
     }
+    OwnedRef flattened;
+    if (PyArray_NDIM(ndarray) != 1) {
+      // GH-49644: a fixed-size list (e.g. fixed-shape-tensor storage) can be
+      // built from a multi-dimensional array, always flattened in C order
+      // regardless of the input's memory layout.

Review Comment:
   I would remove the comment here. Maybe only mention the 0-dimensional case 
we are catching here, in short.



##########
python/pyarrow/src/arrow/python/python_to_arrow.cc:
##########
@@ -908,13 +908,32 @@ class PyListConverter : public ListConverter<T, 
PyConverter, PyConverterTrait> {
 
   Status AppendNdarray(PyObject* value) {
     PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(value);
-    if (PyArray_NDIM(ndarray) != 1) {
-      return Status::Invalid("Can only convert 1-dimensional array values");
-    }
     if (PyArray_ISBYTESWAPPED(ndarray)) {
       // TODO
       return Status::NotImplemented("Byte-swapped arrays not supported");
     }
+    OwnedRef flattened;
+    if (PyArray_NDIM(ndarray) != 1) {
+      // GH-49644: a fixed-size list (e.g. fixed-shape-tensor storage) can be
+      // built from a multi-dimensional array, always flattened in C order
+      // regardless of the input's memory layout.
+      if (PyArray_NDIM(ndarray) < 2 || this->list_type_->id() != 
Type::FIXED_SIZE_LIST) {
+        return Status::Invalid(
+            "Can only convert 1-dimensional array values to a variable-sized 
list");
+      }
+      // Get an aligned, C-contiguous array (copying only if needed), then view
+      // it as 1-D so its values can be read directly in C order.

Review Comment:
   ```suggestion
         // Get an aligned, C-contiguous array (copying only if needed)
   ```
   I think this is sufficient.



##########
python/pyarrow/tests/test_extension_type.py:
##########
@@ -1730,6 +1730,56 @@ def test_tensor_array_from_numpy(np_type_str):
         pa.FixedShapeTensorArray.from_numpy_ndarray(arr, dim_names=[0, 1])
 
 
[email protected]
[email protected]("np_type_str", ("int8", "int64", "float32"))
+def test_tensor_array_from_list_of_ndarrays(np_type_str):
+    # GH-49644: build a fixed-shape-tensor array from a list of individual
+    # (multi-dimensional) ndarrays, not only from a single stacked ndarray.

Review Comment:
   ```suggestion
       # GH-49644
   ```
   
   To me personally, this is enough.



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