Skip to content

[Data][2.56.1] Fix TensorDtype.__from_arrow__ crash on empty tensor columns (#64767) - #64794

Merged
elliot-barn merged 1 commit into
releases/2.56.1from
cherry-pick/2.56.1/pr-64767
Jul 16, 2026
Merged

elliot-barn merged 1 commit into
releases/2.56.1from
cherry-pick/2.56.1/pr-64767

Conversation

@elliot-barn

Copy link
Copy Markdown
Collaborator

Cherry-pick of #64767 onto the releases/2.56.1 branch.

Why are these changes needed?

Fixes a 2.56 regression where converting tensor-extension columns to pandas crashed on zero-size tensor elements. Replaces numpy's -1 dimension inference with an explicit row count and builds the buffer directly for empty arrays instead of using np.stack.

Related issue number

Cherry-pick of #64767

Checks

Cherry-pick applied cleanly (no conflicts). DCO sign-off added.
🤖 Generated with Claude Code

…64767)

## Description

`Dataset.to_pandas()` (and any Arrow → pandas batch conversion) crashes
on Ray 2.56 when a tensor-extension column has zero-size elements, i.e.
`element_shape` contains a `0`. This happens naturally when a per-frame
/ per-group operator emits an empty tensor for some rows (e.g. a
detection op that finds nothing in a frame).

`TensorDtype.__from_arrow__` reshapes the reconstructed values with a
`-1` dimension:

```python
values = values.reshape((-1,) + self.element_shape)
```

numpy cannot infer a `-1` dimension from a size-0 array when another
dimension is `0`, so this raises:

```
ValueError: cannot reshape array of size 0 into shape (0)
```

Minimal repro (fails on 2.56.0, worked on 2.55):

```python
import ray, numpy as np
ray.data.from_items([{"pts": np.zeros((0,), np.float32)} for _ in range(3)]).to_pandas()
```

This regressed in 2.56 via #61733, which added the reshape (2.55's
`__from_arrow__` simply returned `TensorArray(values)` with no reshape).

## Related issues

Closes #64766.

## Additional information

The fix uses the explicit row count instead of `-1` (numpy no longer has
to infer a dimension), and builds the empty buffer directly for the
zero-row `object`-dtype case, since `np.stack` requires at least one
array:

```python
num_rows = len(array)
if values.dtype == object:
    values = (
        np.stack(values)
        if num_rows > 0
        else np.empty((0,) + self.element_shape, dtype=self._dtype)
    )
values = values.reshape((num_rows,) + self.element_shape)
```

Added `test_tensors_in_tables_pandas_roundtrip_empty_elements`,
parametrized over element shapes `(0,)` and `(0, 2)` plus the existing
tensor-format (V1 / V2 / ARROW_NATIVE) and auto-cast fixtures. Verified
locally that the V1 and V2 paths crash before this fix and pass after;
the native `pa.fixed_shape_tensor` path did not crash (it does not route
through `__from_arrow__`) and continues to work.

Signed-off-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
(cherry picked from commit 34607f7)
Signed-off-by: elliot-barn <[email protected]>
@elliot-barn
elliot-barn requested a review from a team as a code owner July 16, 2026 03:08

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where converting empty tensor columns or tensors with zero-size dimensions to Pandas fails because NumPy cannot infer a -1 dimension from a size-0 array. The fix explicitly checks the row count and constructs an empty buffer when the row count is zero, or reshapes using the explicit row count. A regression test is also added. Feedback points out a potential IndexError when handling an empty ChunkedArray with zero chunks, which would fail on array.chunk(0) before reaching the new logic.

# infer a -1 dimension from a size-0 array ("cannot reshape array of
# size 0 into shape (0)"). See
# https://github.com/ray-project/ray/issues/64766.
num_rows = len(array)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There is a potential IndexError when array is an empty pa.ChunkedArray with num_chunks == 0.

In the preceding block (lines 447-466), if array is a pa.ChunkedArray and num_chunks is 0, the condition array.num_chunks > 1 evaluates to False. The code then executes the else branch:

values = array.chunk(0).to_numpy(zero_copy_only=False)

This will raise IndexError: Index 0 out of bounds before reaching the new check num_rows = len(array) at line 477.

To prevent this, we should handle empty chunked arrays or empty arrays early in __from_arrow__, or check array.num_chunks == 0 in the chunked array handling block.

@elliot-barn elliot-barn added the go add ONLY when ready to merge, run all tests label Jul 16, 2026
@elliot-barn
elliot-barn merged commit 6d4f5e6 into releases/2.56.1 Jul 16, 2026
3 checks passed
@elliot-barn
elliot-barn deleted the cherry-pick/2.56.1/pr-64767 branch July 16, 2026 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants