ray icon indicating copy to clipboard operation
ray copied to clipboard

[Data] Converting tensor columns backed by sliced Arrow value arrays to NumPy or pandas returns wrong values

Open m4ru1 opened this issue 6 days ago • 0 comments

What happened + What you expected to happen

Converting a Ray Data tensor column whose underlying Arrow value array is a slice to NumPy or pandas—via Table.to_pandas(), .to_numpy(), or a fused Dataset.map_batches(..., batch_format="numpy" | "pandas") stage—silently returns values from before the slice instead of the sliced values. Reading the same column via to_pylist() or Dataset.take_all() returns the expected values:

values = pa.array([99, 98, 97, 10, 11, 20, 21]).slice(3)  # [10, 11, 20, 21]
col = pa.ListArray.from_arrays([0, 2, 4], values)
table = pa.table({"t": pa.ExtensionArray.from_storage(ArrowTensorType((2,), pa.int64()), col)})
table["t"].to_pylist()             # [array([10, 11]), array([20, 21])]
table.to_pandas()["t"].to_numpy()  # array([[99, 98], [97, 10]])

This happens for example when an Arrow-producing map_batches(..., batch_format="pyarrow") UDF or preprocessing step slices a flat Arrow feature buffer before wrapping it as a tensor column:

  • Fixed-shape and variable-shaped tensor columns (both non-null and null-bearing rows) converted via .to_numpy() or Table.to_pandas()
  • Fused Dataset.map_batches(..., batch_format="pyarrow") stages followed by map_batches(..., batch_format="numpy" | "pandas") before object-store serialization normalizes child slice offsets

Expected behavior: Converting a tensor column backed by a sliced Arrow value array to NumPy or pandas—whether on an Arrow Table or in fused Dataset.map_batches() stages—should return the same tensor values as to_pylist() and Dataset.take_all().

Cause: Buffer-view conversions in ArrowTensorArray and ArrowVariableShapedTensorArray (python/ray/data/_internal/tensor_extensions/arrow.py) compute byte and bit offsets from the list array's row offsets alone, ignoring any slice offset on the underlying child value array.

Versions / Dependencies

  • Ray 3.0.0.dev0 (a872863f2b wheel with master ray.data @ e4462656e8)
  • Python 3.11.15, PyArrow 26.0.1 (also reproduced on 23.0.0 and 26.0.0), NumPy 2.3.5, pandas 3.1.1 (and 2.3.3), macOS (arm64)

Reproduction script

import pyarrow as pa
import ray
from ray.data.extensions import ArrowTensorType, ArrowVariableShapedTensorType


def make_arrow_batch(_=None):
    values = pa.array([99, 98, 97, 10, 11, 20, 21], type=pa.int64()).slice(3)
    fixed_storage = pa.ListArray.from_arrays([0, 2, 4], values)
    var_storage = pa.StructArray.from_arrays(
        [
            pa.LargeListArray.from_arrays([0, 2, 4], values),
            pa.array([[2], [2]], type=pa.list_(pa.int64())),
        ],
        names=["data", "shape"],
    )
    return pa.table(
        {
            "fixed": pa.ExtensionArray.from_storage(
                ArrowTensorType((2,), pa.int64()), fixed_storage
            ),
            "var": pa.ExtensionArray.from_storage(
                ArrowVariableShapedTensorType(pa.int64(), 1), var_storage
            ),
        }
    )


table = make_arrow_batch()
print("to_pylist :", {k: [x.tolist() for x in table[k].to_pylist()] for k in table.column_names})
print("to_pandas :", {k: [x.tolist() for x in table.to_pandas()[k].to_numpy()] for k in table.column_names})

ds = ray.data.range(1).map_batches(make_arrow_batch, batch_format="pyarrow")
print("ds.take_all     :", [r["fixed"].tolist() for r in ds.take_all()])
print(
    "ds numpy batches:",
    [r["fixed"].tolist() for r in ds.map_batches(lambda b: b, batch_format="numpy").take_all()],
)

Output (Ray stderr logs omitted; usage-stats banner depends on local Ray config):

to_pylist : {'fixed': [[10, 11], [20, 21]], 'var': [[10, 11], [20, 21]]}
to_pandas : {'fixed': [[99, 98], [97, 10]], 'var': [[99, 98], [97, 10]]}
Usage stats collection is enabled by default for nightly wheels. To disable this, run the following command: `ray disable-usage-stats` before starting Ray. See https://docs.ray.io/en/master/cluster/usage-stats.html for more details.
ds.take_all     : [[10, 11], [20, 21]]
ds numpy batches: [[99, 98], [97, 10]]

Issue Severity

Medium: It is a significant difficulty but I can work around it.

m4ru1 avatar Nov 28 '26 20:11 m4ru1