Skip to content

[cherry-pick][2.60.0][Data] Keep pandas metadata on Arrow blocks parsable by pandas (#67576) - #67648

Merged
sai-miduthuri merged 1 commit into
releases/2.60.0from
sai-miduthuri/cherry-pick-67576-2.60.0
Oct 31, 2026
Merged

sai-miduthuri merged 1 commit into
releases/2.60.0from
sai-miduthuri/cherry-pick-67576-2.60.0

Conversation

@sai-miduthuri

@sai-miduthuri sai-miduthuri commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Cherry-pick of #67576 (master commit d6974fe) onto releases/2.60.0.

Why

Since #61733 (2.56), the batch a pandas UDF returns becomes an Arrow block built by pa.Table.from_pandas, which records each column's pandas dtype name in the block's metadata. pandas cannot parse the names of parametrized Arrow-backed dtypes. So when enable_arrow_backed_pandas_conversion is off, a UDF that returns an Arrow-backed list, struct or map column makes the next to_pandas() fail with TypeError: data type 'list<item: int64>[pyarrow]' not understood. pd.read_parquet fails the same way on files written from these blocks. A customer hit this upgrading from 2.55 to 2.59.

#67576 rewrites each unparsable dtype name to object when _from_pandas_safe builds the block, and leaves names that parse, such as Int64, alone.

Cherry-pick details

Applied cleanly with git cherry-pick -x. The same 2 files, +114/-1, and the added and removed lines are identical to master's. git patch-id differs only because of one context line: master reformatted the PandasRow docstring in #67539, which isn't on this branch.

No prerequisite commits are missing. After the pick, _from_pandas_safe, _with_parsable_pandas_metadata and _parsable_pandas_metadata are identical to master's.

Not a duplicate: this branch doesn't contain #67576, and no PR against releases/2.60.0 references it. #67649 picks the same commit onto releases/2.59.1.

Testing

Command Result
pre-commit run --files python/ray/data/_internal/pandas_block.py python/ray/data/tests/test_pandas_block.py Passed
pytest python/ray/data/tests/test_pandas_block.py python/ray/data/tests/test_arrow_block.py 102 passed
#67576's repro (pandas UDF returning an Arrow-backed list column, flag off), without and with the pick Without: TypeError: data type 'list<item: int64>[pyarrow]' not understood. With: returns the lists as an object column

I ran these against the releases/2.60.0 release wheel built at the branch head (85ad153), with this branch's python/ray/data copied over it, on Python 3.12 and pandas 2.3.3 (the version CI pins). 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

## Summary

A pandas UDF that returns an Arrow-backed list column makes the next
`to_pandas()` fail with "data type 'list<item: int64>[pyarrow]' not
understood" when `enable_arrow_backed_pandas_conversion` is off. This PR
makes the pandas metadata on blocks built from pandas batches parsable
by pandas, so pyarrow falls back to its own conversion for those
columns.

## Problem

- Since #61733 (2.56), the batch a pandas UDF returns becomes an Arrow
block right away. With no object column, `pa.Table.from_pandas` builds
it and records each column's pandas dtype name in the block's metadata.
- When `to_pandas()` has no types mapper for a column, pyarrow rebuilds
the dtype from that name. pandas cannot parse the names of parametrized
Arrow-backed dtypes, so the conversion raises.
- The flag-off path has no mapper, so any Arrow-backed list, struct or
map column from a UDF breaks it. A `dictionary` Arrow-backed column
breaks the flag-on path too, because the mapper returns `None` for
dictionary types.
- `write_parquet` carries the same metadata into files, and
`pd.read_parquet` fails on them the same way.

```python
import pyarrow as pa, pandas as pd, ray

ray.data.DataContext.get_current().enable_arrow_backed_pandas_conversion = False

def udf(df):
    return pd.DataFrame({"x": pd.array([[1, 2], [3]], dtype=pd.ArrowDtype(pa.list_(pa.int64())))})

ray.data.from_items([{"x": 1}, {"x": 2}]).map_batches(udf, batch_format="pandas").to_pandas()
```

| | Result |
|---|---|
| 2.55 | works, the batch stays a pandas block |
| 2.56 to master | `TypeError: data type 'list<item: int64>[pyarrow]'
not understood` |
| After | works, `x` is an object column of arrays |

## Change

A pandas batch becomes an Arrow block with a note of each column's
pandas dtype. One note is unreadable, so the next `to_pandas()` crashes.
The fix rewrites just that note when the block is built.

```
column   pandas dtype note          before          after
x        list<item: int64>[pyarrow]   TypeError       object (pyarrow's default)
n        Int64                        Int64           Int64 (unchanged)
```

- `_from_pandas_safe` rewrites each dtype name pandas cannot parse to
`object` on the fast path. The slow path builds the table column by
column and never had metadata.
- Names that parse are left alone, so the flag-off path still restores
nullable dtypes such as `Int64`, `boolean` and `string`.
- The rewrite is cached on the metadata bytes. For scale: the uncached
check on a 51-column schema costs about as much as the `from_pandas`
call it follows, and the cached path is a dictionary lookup.

## Decisions to confirm

- Rewriting only the unparsable names, rather than dropping the metadata
or passing `ignore_metadata=True`. Dropping it would turn a UDF's
`Int64` column with nulls into `float64` on the flag-off path, which
2.56 to 2.59 restore today.
- Doing it where the block is built, rather than in `to_pandas()`.
Blocks are built once and read many times, and files written from them
become readable by pandas.

## Subsystems

Block / data format (`pandas_block.py`).

## Related

- Reported by a customer upgrading from 2.55 to 2.59, found while fixing
#67566.
- The same metadata path broke the first version of #67566; this PR
covers the case that version left behind.
- #56402 added `pandas_block_ignore_metadata` for tensor dtype names in
the same metadata; it is off by default.
- Upstream: pandas cannot parse its own Arrow-backed dtype names,
tracked in pandas-dev/pandas#57411. The pandas fix,
pandas-dev/pandas#66758, is on hold behind the wider pyarrow
compatibility work in pandas-dev/pandas#59780, so Ray rewrites the names
on its side until a pandas release carries it.
- Not a duplicate: no open issue or PR covers unparsable pandas dtype
names on blocks.

## Testing

The new tests fail without the fix and pass with it:

```
pytest python/ray/data/tests/test_pandas_block.py python/ray/data/tests/test_arrow_block.py   # 103 passed
pytest python/ray/data/tests/test_map_batches.py python/ray/data/tests/datasource/test_parquet.py \
       python/ray/data/tests/test_transform_pyarrow.py python/ray/data/tests/preprocessors   # 1474 passed, 11 skipped
```

AI assistance (Claude Code) was used to find the root cause and write
this change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
(cherry picked from commit d6974fe)
Signed-off-by: Sai Miduthuri <[email protected]>
@sai-miduthuri
sai-miduthuri requested a review from a team as a code owner October 31, 2026 01:17
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@sai-miduthuri sai-miduthuri added the go add ONLY when ready to merge, run all tests label Oct 31, 2026
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Oct 31, 2026
@sai-miduthuri
sai-miduthuri merged commit b7f4259 into releases/2.60.0 Oct 31, 2026
7 checks passed
@sai-miduthuri
sai-miduthuri deleted the sai-miduthuri/cherry-pick-67576-2.60.0 branch October 31, 2026 20:38
elliot-barn pushed a commit that referenced this pull request Oct 31, 2026
…able by pandas (#67576) (#67649)

Cherry-pick of #67576 (master commit
d6974fe) onto `releases/2.59.1`.

## Why

Since #61733 (2.56), the batch a pandas UDF returns becomes an Arrow
block built by `pa.Table.from_pandas`, which records each column's
pandas dtype name in the block's metadata. pandas cannot parse the names
of parametrized Arrow-backed dtypes. So when
`enable_arrow_backed_pandas_conversion` is off, a UDF that returns an
Arrow-backed list, struct or map column makes the next `to_pandas()`
fail with `TypeError: data type 'list<item: int64>[pyarrow]' not
understood`. `pd.read_parquet` fails the same way on files written from
these blocks. A customer hit this upgrading from 2.55 to 2.59.

#67576 rewrites each unparsable dtype name to `object` when
`_from_pandas_safe` builds the block, and leaves names that parse, such
as `Int64`, alone.

## Cherry-pick details

Picked with `git cherry-pick -x`. `pandas_block.py` applied cleanly.
`test_pandas_block.py` conflicted in two places, because master's copy
has `PandasRow` tests and an import from #66116 and #66219, which aren't
on this branch. I resolved it by keeping only #67576's lines: the
`_parsable_pandas_metadata` import, the `_pandas_metadata_dtype_names`
helper and the three new tests. In both files, the added and removed
lines are identical to master's, and the commit is +114/-1 like #67576.

No prerequisite commits are missing. After the pick,
`_from_pandas_safe`, `_with_parsable_pandas_metadata` and
`_parsable_pandas_metadata` are identical to master's, and the
`restore_data_context` and `ray_start_regular_shared` fixtures the new
tests use exist on this branch.

Not a duplicate: this branch doesn't contain #67576, and no PR against
`releases/2.59.1` references it. #67648 picks the same commit onto
`releases/2.60.0`.

## Testing

| Command | Result |
|---|---|
| `pre-commit run --files python/ray/data/_internal/pandas_block.py
python/ray/data/tests/test_pandas_block.py` | Passed |
| `pytest python/ray/data/tests/test_pandas_block.py
python/ray/data/tests/test_arrow_block.py` | 83 passed |
| #67576's repro (pandas UDF returning an Arrow-backed list column, flag
off), without and with the pick | Without: `TypeError: data type
'list<item: int64>[pyarrow]' not understood`. With: returns the lists as
an `object` column |

I ran these against PyPI's `ray==2.59.0` wheel with this branch's
`python/ray/data` copied over it, on Python 3.12 and pandas 2.3.3 (the
version CI pins). `python/ray/data` has no changes between `ray-2.59.0`
and `releases/2.59.1`. 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 Fable 5.1 <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues 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