Skip to content

[Data] Respect child value-array slice offsets when converting tensor columns to NumPy and pandas - #68633

Open
m4ru1 wants to merge 1 commit into
ray-project:masterfrom
m4ru1:fix/tensor-sliced-child-offset
Open

m4ru1 wants to merge 1 commit into
ray-project:masterfrom
m4ru1:fix/tensor-sliced-child-offset

Conversation

@m4ru1

@m4ru1 m4ru1 commented Nov 28, 2026

Copy link
Copy Markdown

Summary

Fix incorrect tensor values returned by Table.to_pandas(), .to_numpy(), and fused NumPy/pandas Dataset.map_batches() stages when a tensor column's underlying Arrow value array is sliced, closing #68632. Whereas #66122 and #67283 built and reused dense buffer views over unsliced value arrays (and #66997 updates pandas TensorArray.take), this accounts for non-zero slice offsets on the Arrow child value array itself.

Before

When an Arrow Table or Dataset contains a fixed-shape or variable-shaped tensor column backed by a sliced Arrow value array, Table.to_pandas(), .to_numpy(), and fused Dataset.map_batches(..., batch_format="numpy" | "pandas") stages index into the underlying data buffer using only list row offsets without the child value array's slice offset, returning values from preceding un-sliced bytes.

After

Anchor tensor buffer views at the combined list row offset and child value-array slice offset across both fixed-shape and variable-shaped tensor conversions, sharing a single buffer-view path that also uses integer element counts when unpacking 0-dimensional boolean rows.

Single-chunk contiguous non-boolean columns retain zero-copy views over the shared Arrow buffer, null-bearing and ragged columns keep per-row views with None for null rows, and boolean columns continue unpacking bits into separate storage. PyArrow's native pa.fixed_shape_tensor path is unchanged.

flowchart LR
  subgraph Before["Before (Block / data format)"]
    B1["Tensor buffer view (row offset only)"] -. ignores child slice start .-> B2["to_pandas / NumPy batches read prefix values"]
  end
  subgraph After["After (Block / data format)"]
    A1["Tensor buffer view (row + child slice offsets)"] -. starts at sliced buffer position .-> A2["to_pandas & NumPy/pandas batches match to_pylist()"]
  end
  classDef bad fill:#ffcdd2,stroke:#c62828,color:#000
  classDef changed fill:#fff9c4,stroke:#f9a825,color:#000
  classDef good fill:#c8e6c9,stroke:#2e7d32,color:#000
  class B1,B2 bad
  class A1 changed
  class A2 good
Loading

Subsystems

Block / data format.

Testing

Local validation passed on Python 3.11.15 using the Ray 3.0.0.dev0 wheel (a872863f2b) with updated ray.data sources (e4462656e8 + working diff) loaded on both driver and workers across PyArrow 23.0.0, 26.0.0, and 26.0.1. Tests check V1/V2 fixed-shape and variable-shaped conversions across numeric, byte-crossing boolean, Unicode, 0-dimensional boolean, null-bearing, multi-chunk, zero-copy memory-sharing, and fused worker Dataset pipelines.

for test in test_tensor_extension.py unit/test_chunked_tensor_take.py \
  unit/test_chunked_variable_tensor_take.py test_tensor.py; do
  python -m pytest -q "python/ray/data/tests/$test" || exit 1
done

Pre-commit hooks (including pyrefly-data) and pytest lint checks also passed.

AI assistance was used.

… columns to NumPy and pandas

Fix incorrect tensor values returned by Table.to_pandas(), .to_numpy(),
and fused Dataset NumPy/pandas map_batches() stages when a fixed-shape
or variable-shaped tensor column is backed by a sliced Arrow value array,
and use integer element counts when unpacking 0-dimensional boolean
tensor rows.

Closes ray-project#68632

Signed-off-by: mora <[email protected]>
@m4ru1
m4ru1 requested a review from a team as a code owner November 28, 2026 20:38

@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 refactors Arrow tensor-to-NumPy conversions to correctly account for child array offsets when dealing with sliced arrays, ensuring zero-copy views remain accurate. It also adds comprehensive unit tests for sliced child tensors under various formats. The review feedback correctly identifies that pa.repeat is used in the new test helpers instead of pa.compute.repeat, which would result in an AttributeError.

storage = tensors.storage
variable_shaped = isinstance(tensors, ArrowVariableShapedTensorArray)
data = storage.field("data") if variable_shaped else storage
values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9)

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.

medium

Using pa.repeat will raise an AttributeError because repeat is not in the top-level pyarrow namespace. It should be pa.compute.repeat instead.

Suggested change
values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9)
values = pa.concat_arrays([pa.compute.repeat(data.values[0], 9), data.values]).slice(9)

storage = tensors.storage
variable_shaped = isinstance(tensors, ArrowVariableShapedTensorArray)
data = storage.field("data") if variable_shaped else storage
values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9)

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.

medium

Using pa.repeat will raise an AttributeError because repeat is not in the top-level pyarrow namespace. It should be pa.compute.repeat instead.

Suggested change
values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9)
values = pa.concat_arrays([pa.compute.repeat(data.values[0], 9), data.values]).slice(9)

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant