GH-49644: [Python] Support converting list of multi-dimensional array…#50203
Open
aboderinsamuel wants to merge 3 commits into
Open
GH-49644: [Python] Support converting list of multi-dimensional array…#50203aboderinsamuel wants to merge 3 commits into
aboderinsamuel wants to merge 3 commits into
Conversation
|
|
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for constructing fixed-shape tensors and fixed-size lists from lists of multi-dimensional NumPy ndarrays by flattening values in C order (GH-49644).
Changes:
- Add tests covering tensor arrays built from lists of ndarrays (including nulls and shape mismatch).
- Add tests ensuring fixed-size lists accept multi-dimensional ndarray elements (and reject invalid cases).
- Update ndarray-to-list conversion to allow flattening for
FIXED_SIZE_LISTwhile keeping variable-sized lists restricted to 1D.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/pyarrow/tests/test_extension_type.py | Adds coverage for building FixedShapeTensorArray from a list of ndarrays. |
| python/pyarrow/tests/test_array.py | Adds a regression test for fixed-size list conversion from multi-dimensional ndarrays. |
| python/pyarrow/src/arrow/python/python_to_arrow.cc | Implements multi-dimensional ndarray flattening for fixed-size lists during conversion. |
… arrays to FixedShapeTensor
…cover C-order flatten
90c2ac4 to
e695d01
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
Constructing a fixed-shape-tensor array from a list of individual ndarrays only
worked when each element was 1-D; ≥2-D elements failed with
ArrowInvalid: Can only convert 1-dimensional array values. The only workaroundwas stacking the list into a single ndarray and using
FixedShapeTensorArray.from_numpy_ndarray.What changes are included in this PR?
The C++ list converter
PyListConverter::AppendNdarraynow acceptsmulti-dimensional ndarray elements for fixed-size lists (the storage of a
fixed-shape tensor) by flattening them in C order. The fixed-size-list builder
still validates that the flattened length matches the list width, so wrong sizes
error cleanly. Variable-sized lists remain restricted to 1-D values to avoid
ambiguity. As a side benefit, plain
fixed_size_listalso acceptsmulti-dimensional ndarray elements now.
Are these changes tested?
Yes:
test_tensor_array_from_list_of_ndarrays— construction from 2-D and 3-Dndarrays, null handling, storage parity with
from_numpy_ndarray, and thesize-mismatch error, across
int8/int64/float32.test_fixed_size_list_from_multidim_ndarray— plainfixed_size_listfrommulti-dim arrays, plus a check that variable-sized lists still reject 2-D.
Are there any user-facing changes?
Yes —
pa.array([multi-dim ndarrays], type=fixed_shape_tensor(...))(and thesame for
fixed_size_list) now works instead of raising. Existing 1-D behaviorand variable-sized-list behavior are unchanged.
Scoped to construction only; the reverse
to_numpyshape-preservation alsoraised in the issue is intentionally left as a separate follow-up.