rok commented on code in PR #50203:
URL: https://github.com/apache/arrow/pull/50203#discussion_r3468744523
##########
python/pyarrow/tests/test_array.py:
##########
@@ -2924,6 +2924,36 @@ def test_array_from_invalid_dim_raises():
pa.array(arr0d)
[email protected]
+def test_fixed_size_list_from_multidim_ndarray():
+ # GH-49644: a fixed-size list can be built from multi-dimensional ndarray
+ # elements by flattening them in C order.
+ arr = pa.array([np.array([[1, 2, 3]], dtype=np.int64),
+ np.array([[4, 5, 6]], dtype=np.int64)],
+ type=pa.list_(pa.int64(), 3))
+ assert arr.type == pa.list_(pa.int64(), 3)
+ assert arr.to_pylist() == [[1, 2, 3], [4, 5, 6]]
+
+ # A non-trivial 2D shape confirms values are flattened in C (row-major)
order
+ arr = pa.array([np.array([[1, 2], [3, 4]], dtype=np.int64)],
+ type=pa.list_(pa.int64(), 4))
+ assert arr.to_pylist() == [[1, 2, 3, 4]]
+
+ # The flattened length must still match the fixed size
+ with pytest.raises(pa.lib.ArrowInvalid):
+ pa.array([np.array([[1, 2], [3, 4]], dtype=np.int64)],
+ type=pa.list_(pa.int64(), 3))
+
+ # Variable-sized lists still require 1-dimensional values
+ with pytest.raises(pa.lib.ArrowInvalid, match="1-dimensional"):
+ pa.array([np.array([[1, 2, 3]], dtype=np.int64)],
+ type=pa.list_(pa.int64()))
+
+ # 0-dimensional arrays are still rejected (not flattened to length 1)
+ with pytest.raises(pa.lib.ArrowInvalid, match="1-dimensional"):
+ pa.array([np.array(1, dtype=np.int64)], type=pa.list_(pa.int64(), 1))
Review Comment:
I'm curious if we can have these.
--
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]