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


##########
python/pyarrow/tests/test_extension_type.py:
##########
@@ -1319,38 +1319,65 @@ def test_tensor_type():
 
 
 def test_tensor_class_methods():
+    from numpy.lib.stride_tricks import as_strided
+
+    values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    np_arr = np.array(values, dtype=np.int8)
     tensor_type = pa.fixed_shape_tensor(pa.float32(), [2, 3])
-    storage = pa.array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]],
-                       pa.list_(pa.float32(), 6))
+    storage = pa.array(np_arr.reshape(2, 6).tolist(), pa.list_(pa.float32(), 
6))
     arr = pa.ExtensionArray.from_storage(tensor_type, storage)
-    expected = np.array(
-        [[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]], dtype=np.float32)
-    result = arr.to_numpy_ndarray()
-    np.testing.assert_array_equal(result, expected)
 
-    expected = np.array([[[1, 2, 3], [4, 5, 6]]], dtype=np.float32)
+    # TODO: add more get_tensor tests
+    assert arr.get_tensor(0) == pa.Tensor.from_numpy(
+        np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
+
+    expected = np_arr.reshape(2, 2, 3).astype(np.float32).tolist()
+    np.testing.assert_array_equal(arr.to_tensor(), expected)
+    np.testing.assert_array_equal(arr.to_numpy_ndarray(), expected)
+
+    # TODO: offset not correctly handled
+    expected = np_arr[6:].reshape(1, 2, 3).astype(np.float32)
+    # expected = np.array([[[7, 8, 9], [10, 11, 12]]], dtype=np.float32)
     result = arr[:1].to_numpy_ndarray()
-    np.testing.assert_array_equal(result, expected)
+    # np.testing.assert_array_equal(result, expected)

Review Comment:
   Didn't check in detail if that's the issue here, but pay attention to the 
offset of the ListArray versus the offset of the underlying values array (which 
then further depends on whether you ask for `values()` or `Flatten()`). 
   
   Python example (which I think should translate to C++ more or less directly):
   
   ```
   In [31]: arr = pa.array([[1, 2], [3, 4]], pa.list_(pa.int64(), 2))
   
   In [32]: arr.slice(1, 1)
   Out[32]: 
   <pyarrow.lib.FixedSizeListArray object at 0x7f283ce57d00>
   [
     [
       3,
       4
     ]
   ]
   
   In [33]: arr.slice(1, 1).offset
   Out[33]: 1
   
   In [34]: arr.slice(1, 1).flatten()
   Out[34]: 
   <pyarrow.lib.Int64Array object at 0x7f2836df4fa0>
   [
     3,
     4
   ]
   
   In [35]: arr.slice(1, 1).flatten().offset
   Out[35]: 2
   
   In [36]: arr.slice(1, 1).values
   Out[36]: 
   <pyarrow.lib.Int64Array object at 0x7f283ce56200>
   [
     1,
     2,
     3,
     4
   ]
   
   In [37]: arr.slice(1, 1).values.offset
   Out[37]: 0
   ```



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to