Repository navigation
[cherry-pick][2.60.1][Data] Keep every element of nested-null columns in Arrow-backed to_pandas (#67566) - #67994
Merged
sai-miduthuri merged 1 commit intoNov 13, 2026
Conversation
…andas (#67566) ## Summary A pandas batch silently loses elements from a column whose type nests Arrow's null type, such as `list<null>`. This PR converts those columns to Arrow-backed pandas columns without the cast that drops the elements. ## Problem - Since 2.56, `ArrowBlockAccessor.to_pandas()` maps each column to `pd.ArrowDtype`. Before pandas 3.1, `pd.ArrowDtype.__from_arrow__` casts every column, even to its own type. - Arrow's cast of a nested null child resizes that child to the number of rows. Elements past that point disappear. - A list of all-null values is typed `list<null>` when no row has a real value. Users see it with sparse list columns. ```python import pyarrow as pa, ray t = pa.table({"x": pa.array([[None, None], [None]], type=pa.list_(pa.null()))}) ds = ray.data.from_arrow(t).map_batches( lambda df: {"x": [list(v) for v in df["x"]]}, batch_format="pandas" ) print(ds.take_all()) ``` | | Output | |---|---| | Current (pandas < 3.1) | `[{'x': [None, None]}, {'x': []}]` | | Expected | `[{'x': [None, None]}, {'x': [None]}]` | When the batch goes back to Arrow untouched, the same column fails instead: `ArrowInvalid: Length spanned by list offsets (3) larger than values array (length 2)`. ## Change ``` Before After Arrow block ──types_mapper──▶ pd.ArrowDtype Arrow block ──types_mapper──▶ _ArrowDtypeWithoutIdentityCast │ │ __from_arrow__: cast(list<null>) __from_arrow__: same type, no cast │ │ ▼ ▼ list<null>[pyarrow], child cut to N rows list<null>[pyarrow], every element kept ``` - `_arrow_backed_pandas_dtype` returns `_ArrowDtypeWithoutIdentityCast` for a type with a null type nested anywhere inside it. The subclass's `__from_arrow__` skips the cast to the column's own type, which is what pandas 3.1 does for every column. - The column it builds carries a plain `pd.ArrowDtype`, and the subclass compares equal to that dtype, so users see no dtype change. - pyarrow handles the column like any other. In particular it never reads the block's pandas metadata for it, which names a dtype string pandas cannot parse (`list<item: null>[pyarrow]`). That is what broke the first version of this PR on a block coming back from a pandas UDF. - Other columns are untouched. With `enable_arrow_backed_pandas_conversion=False`, nothing changes. ## Decisions to confirm - The fix skips the identity cast for nested-null types only. Returning the subclass for every column would match pandas 3.1 exactly and drop `_has_nested_null_type`, at the cost of changing the conversion path of every column on pandas < 3.1. - A top-level `struct<a: null>` casts correctly but takes the new path too, because one rule ("any nested null") is simpler to reason about. ## Subsystems Block / data format (`arrow_block.py`). ## Related - Reported by a customer upgrading from 2.55 to 2.59. - Follows #64768, which added `enable_arrow_backed_pandas_conversion`. - Not a duplicate: no open issue or PR covers nested null types in `to_pandas()`. - Fixed in #67576, now merged; this branch carries it, which the pyarrow 17 CI job needs: with the flag off, a pandas UDF that returns its own `pd.ArrowDtype` list column makes the next `to_pandas()` fail with the same "not understood" error. Since #61733 the UDF's batch becomes an Arrow block with pandas metadata, and with no mapper pyarrow reads that metadata. On 2.55 the batch stayed a pandas block and it worked. ## Testing The three nested-null cases of the round-trip test fail without the fix and pass with it. The sliced `list<null>` case aborts the worker inside Arrow on master (`Slice offset (3) greater than array length (2)`), which is what a pandas UDF hits when `batch_size` is smaller than the block. Run locally with pandas 2.3.3 and pyarrow 23.0.1: ``` pytest python/ray/data/tests/test_arrow_block.py \ python/ray/data/tests/test_pandas_block.py \ python/ray/data/tests/test_transform_pyarrow.py \ python/ray/data/tests/preprocessors --ignore=.../test_torch.py # 563 passed, 5 skipped pytest python/ray/data/tests/test_map_batches.py python/ray/data/tests/test_batcher.py # 766 passed, 1 failed (test_local_shuffle_buffer_warns_if_too_large: OutOfDiskError from a full local disk, fails the same way without this change) ``` The `__from_arrow__` and equality behaviour of the subclass was also checked on pandas 2.2.3 (Ray's floor) and 3.1.0 against every type in the parametrized test. AI assistance (Claude Code) was used to reproduce the bug and write this change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Marwan Sarieddine <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit af8f74e) Signed-off-by: Sai Miduthuri <[email protected]>
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a workaround for handling nested null types in Arrow-backed pandas columns, specifically to address issues with identity casting in pandas versions prior to 3.1. It adds a custom _ArrowDtypeWithoutIdentityCast class and updates _arrow_backed_pandas_dtype to use this class for nested null types. Additionally, comprehensive tests have been added to verify that these types round-trip correctly and maintain equality with standard pd.ArrowDtype instances. I have no feedback to provide as there were no review comments.
AarryaSaraf
approved these changes
Nov 13, 2026
sai-miduthuri
added a commit
that referenced
this pull request
Nov 13, 2026
… in Arrow-backed to_pandas (#67566) (#68022) ## Description Cherry-pick of #67566 (master commit af8f74e) onto `releases/2.59.2`. Since 2.56, `ArrowBlockAccessor.to_pandas()` maps each column to `pd.ArrowDtype`. Before pandas 3.1, `pd.ArrowDtype.__from_arrow__` casts every column, even to its own type. Arrow's cast of a nested null child resizes that child to the number of rows, so elements disappear. For example, a `list<null>` column `[[None, None], [None]]` comes back as `[[None, None], []]`. When the batch goes back to Arrow untouched, it fails instead with `ArrowInvalid: Length spanned by list offsets (3) larger than values array (length 2)`. Users hit this with sparse list columns. #67566 maps types that nest a null type to a `pd.ArrowDtype` subclass that skips the identity cast. ## Related issues Related to #67566. The same pick for 2.60.1 is #67994. ## Additional information ### Cherry-pick details Applied with `git cherry-pick -x`. `arrow_block.py` merged cleanly. `test_arrow_block.py` conflicted only on its import list: master also imports `ArrowRow` there, from a master-only change that this branch's tests don't use. I added only #67566's `_arrow_backed_pandas_dtype` import. The added and removed lines are identical to the master commit, and `git patch-id` differs only because of that context line. No prerequisite commits are missing. The new code hooks into `_arrow_backed_pandas_dtype`, which this branch already has from the #66578 pick (#66617). The two files differ from master at af8f74e only by master commits this change doesn't use. This PR and the #67579 pick (#68025) both edit `arrow_block.py` and `test_arrow_block.py`. This commit merges cleanly with the #66521, #67579 and #67806 stack and passes the tests below. Not a duplicate: this branch doesn't contain #67566, and no PR against `releases/2.59.2` references it. #67994 picks the same commit onto `releases/2.60.1`. ### Testing | Command | Result | |---|---| | `pre-commit run --files <the 2 changed files>` | Passed | | `pytest python/ray/data/tests/test_arrow_block.py python/ray/data/tests/test_pandas_block.py` | 87 passed | | `pytest python/ray/data/tests/test_arrow_block.py`, run with this PR's tests against the branch code without the fix | 3 failed (the 3 new nested-null cases of `test_arrow_block_to_pandas_preserves_arrow_types_through_roundtrip`), 24 passed | | `pytest test_arrow_block.py test_pandas_block.py test_tensor_extension.py`, with the #66521, #67579 and #67806 stack also applied | 365 passed | I ran these against the Ray 2.59.1 release wheel, with this branch's `python/ray/data` and `python/ray/tests` copied over it. The wheel was built from `releases/2.59.1` at 7bdaad7, and its Python sources match `releases/2.59.2` except for `_version.py`. The environment was Python 3.12 with pandas 2.3.3, pyarrow 23.0.1 and numpy 2.1.3, the versions CI pins on this branch. Building Ray from source for `bazel test` wasn't practical on my machine. AI assistance was used to prepare this cherry-pick. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Marwan Sarieddine <[email protected]> Signed-off-by: Sai Miduthuri <[email protected]> Co-authored-by: Marwan Sarieddine <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
sai-miduthuri
added a commit
that referenced
this pull request
Nov 13, 2026
…ble across blocks (#67579) (#67999) ## Description Cherry-pick of #67579 (master commit 1acc2c3) onto `releases/2.60.1`. **Middle of a 3-PR stack on `releases/2.60.1`:** #67995 (the #66521 pick), then this PR, then #68019 (the #67806 pick, which fixes this change on pyarrow 26). Merge the stack with `gh stack merge 68019`, which lands all three in order. Since #61733 (2.56), a batch that spans blocks whose tensor column is null in one of them fails with `ValueError: Trying to unify unsupported tensor type: null`. A pandas UDF that returns `None` for some rows and an ndarray for others is enough to hit it. It worked in 2.55. #67579 lets tensor arrays carry null rows, so these blocks merge, the column stays a tensor column, and missing rows read back as `None`. ## Related issues Related to #67579. Depends on #67995, the #66521 pick. ## Additional information ### Cherry-pick details Applied with `git cherry-pick -x`. 9 of the 10 files merged cleanly. `tensor_extensions/pandas.py` conflicted only on the `TensorArray.dtype` docstring. Master had reformatted it in #67539 (ruff D212/D200), which isn't on this branch. I kept this branch's docstring and added the PR's one new line, `first = _first_non_null(self._tensor)`. The added and removed lines are identical to the master commit. `git patch-id` differs only because of that docstring context line. Prerequisite: #66521. Without it, `test_arrow_concat_tensor_with_null_keeps_tensor_type[last_shape1]` and `test_unify_schemas_tensor_with_null_keeps_tensor_type[other_shape1]` fail on this branch. These are the cases where the tensor blocks have different shapes. I applied each master-only commit that touched `transform_pyarrow.py` or the tensor extensions between the branch cut and #67579 on top of this pick. #66521 alone fixes both tests. The other files differ from master only by master commits this change doesn't use. Not a duplicate: this branch doesn't contain #67579, and no PR against `releases/2.60.1` references it. ### Testing All of these ran with the #66521 pick applied underneath. | Command | Result | |---|---| | `pre-commit run --files <the 10 changed files>` | Passed | | `pytest test_arrow_block.py test_tensor_extension.py test_torch_tensor_utils.py unit/test_transform_pyarrow.py test_transform_pyarrow.py test_pandas_block.py` (under `python/ray/data/tests/`) | 526 passed, 3 skipped | | The same test files from this PR, against the code with only #66521 (no #67579) | 60 failed, 155 passed, and `test_tensor_extension.py` fails to collect, because it imports a helper this PR adds | | This pick directly on `releases/2.60.1`, without #66521 | The 2 tests above fail | | `test_arrow_block.py test_pandas_block.py test_tensor_extension.py` with the #67566 pick (#67994) also applied | 397 passed | `test_tensor_column_no_memory_leak` needs `scipy`, which my environment didn't have at first. The 526 passed is from a run with scipy installed. I ran these against the Ray 2.60.0 release wheel, with this branch's `python/ray/data` and `python/ray/tests` copied over it. The wheel was built from `releases/2.60.0` at b7f4259, and its Python sources match `releases/2.60.1` except for `_version.py`. The environment was Python 3.12 with pandas 2.3.3 and pyarrow 23.0.1, the versions CI pins, plus numpy 2.2.6 and torch 2.9.0+cpu. Building Ray from source for `bazel test` wasn't practical on my machine. AI assistance was used to prepare this cherry-pick. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Marwan Sarieddine <[email protected]> Signed-off-by: Elliot Barnwell <[email protected]> Signed-off-by: Sai Miduthuri <[email protected]> Co-authored-by: Marwan Sarieddine <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]> Co-authored-by: Elliot Barnwell <[email protected]>
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.
Description
Cherry-pick of #67566 (master commit af8f74e) onto
releases/2.60.1.Since 2.56,
ArrowBlockAccessor.to_pandas()maps each column topd.ArrowDtype. Before pandas 3.1,pd.ArrowDtype.__from_arrow__casts every column, even to its own type. Arrow's cast of a nested null child resizes that child to the number of rows, so elements disappear. For example, alist<null>column[[None, None], [None]]comes back as[[None, None], []]. When the batch goes back to Arrow untouched, it fails instead withArrowInvalid: Length spanned by list offsets (3) larger than values array (length 2). Users hit this with sparse list columns.#67566 maps types that nest a null type to a
pd.ArrowDtypesubclass that skips the identity cast.Related issues
Related to #67566.
Additional information
Cherry-pick details
Applied cleanly with
git cherry-pick -x. It has the same 2 files, +89/-2, and the samegit patch-id --stableas the master commit.No prerequisite commits are missing. The new code hooks into
_arrow_backed_pandas_dtype, which this branch already has from the #66578 pick (#66644). The two files differ from master at af8f74e only by master commits this change doesn't use.This PR and the #67579 pick both edit
arrow_block.pyandtest_arrow_block.py. I applied this commit on top of the #66521 and #67579 picks, and it merges cleanly and passes the tests below.Not a duplicate: this branch doesn't contain #67566, and no PR against
releases/2.60.1references it.Testing
pre-commit run --files <the 2 changed files>pytest python/ray/data/tests/test_arrow_block.py python/ray/data/tests/test_pandas_block.pypytest python/ray/data/tests/test_arrow_block.py, run with this PR's tests against the branch code without the fixtest_arrow_block_to_pandas_preserves_arrow_types_through_roundtrip), 31 passedpytest test_arrow_block.py test_pandas_block.py test_tensor_extension.py, with the #66521 and #67579 picks also appliedI ran these against the Ray 2.60.0 release wheel, with this branch's
python/ray/dataandpython/ray/testscopied over it. The wheel was built fromreleases/2.60.0at b7f4259, and its Python sources matchreleases/2.60.1except for_version.py. The environment was Python 3.12 with pandas 2.3.3 and pyarrow 23.0.1, the versions CI pins. Building Ray from source forbazel testwasn't practical on my machine.AI assistance was used to prepare this cherry-pick.
🤖 Generated with Claude Code