Skip to content

[data] ignore metadata for pandas block - #56402

Merged
alexeykudinkin merged 5 commits into
ray-project:masterfrom
iamjustinhsu:jhsu/fix-tensor-strings
Sep 11, 2025
Merged

alexeykudinkin merged 5 commits into
ray-project:masterfrom
iamjustinhsu:jhsu/fix-tensor-strings

Conversation

@iamjustinhsu

@iamjustinhsu iamjustinhsu commented Sep 10, 2025 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Consider the following the code

import ray
# Read File (1)
source_path = "file_that_contains_tensor_strings.parquet"
ds = ray.data.read_parquet(source_path)
# Write File (2)
dest_path = "/tmp"
ds.map_batches(..., batch_format="pandas").write_parquet(dest_path)

# Read File Again (3)
new_ds = ray.data.read_parquet(dest_path).map_bataches(..., batch_format="pandas")

At a high level we read, write, read. On a lower-level, we convert arrow blocks -> pandas -> arrow blocks -> pandas. We have connectors and registered extension types in python/ray/air/util/tensor_extensions/, however we special case handle tensor types by converting them to TensorArrays here when we convert pandas -> arrow. During this process, however, pyarrow will store metadata about the pandas block, which will look something like this:

{
    "name": "feature1",
    "field_name": "feature1",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8, 2), dtype=<U38)",
    "metadata": null
},
{
    "name": "feature2",
    "field_name": "feature2",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8,), dtype=float32)",
    "metadata": null
}

For the most part this is fine, however, when converting back to pandas, arrow will first attempt to search through the metadata("numpy_type") to restore the schema. This can be troublesome because pandas/numpy doesn't know how to handle those custom types.

In pyarrow==14.0.0, this is an issue, because it surrenders the special casing to numpy/pandas
in pyarrow==21.0.0, it's smarter and DOES handle that (I tested this)

NOTE

This has been tested with pyarrow==14.0.0 (ray version 2.45) and this works.

Solution

To handle older pyarrow versions, we can do ignore_metadata=True

Related issue number

Checks

@iamjustinhsu
iamjustinhsu force-pushed the jhsu/fix-tensor-strings branch from 7298367 to 1f7dcec Compare September 10, 2025 00:23
@iamjustinhsu iamjustinhsu added the go add ONLY when ready to merge, run all tests label Sep 10, 2025
@iamjustinhsu
iamjustinhsu marked this pull request as ready for review September 10, 2025 19:18
@iamjustinhsu
iamjustinhsu requested a review from a team as a code owner September 10, 2025 19:18
Signed-off-by: iamjustinhsu <[email protected]>
@iamjustinhsu
iamjustinhsu requested a review from a team as a code owner September 10, 2025 20:07
Signed-off-by: iamjustinhsu <[email protected]>
arrow_table = pa.Table.from_pandas(df_pandas)

# Convert back to pandas
df_roundtrip = arrow_table.to_pandas(ignore_metadata=True)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

confirmed this will fail without ignore_metadata=True. I wrote this test here to show that iamjustinhsu#3 will solve the issue.

Signed-off-by: iamjustinhsu <[email protected]>
@iamjustinhsu
iamjustinhsu force-pushed the jhsu/fix-tensor-strings branch from 52f2784 to f6fc010 Compare September 10, 2025 20:15
Signed-off-by: iamjustinhsu <[email protected]>

DEFAULT_ENABLE_PANDAS_BLOCK = True

DEFAULT_PANDAS_BLOCK_IGNORE_METADATA = bool(

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.

Let's use env_bool

@alexeykudinkin
alexeykudinkin merged commit 0047e72 into ray-project:master Sep 11, 2025
ZacAttack pushed a commit to ZacAttack/ray that referenced this pull request Sep 24, 2025
<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?
Consider the following the code
```python
import ray
# Read File (1)
source_path = "file_that_contains_tensor_strings.parquet"
ds = ray.data.read_parquet(source_path)
# Write File (2)
dest_path = "/tmp"
ds.map_batches(..., batch_format="pandas").write_parquet(dest_path)

# Read File Again (3)
new_ds = ray.data.read_parquet(dest_path).map_bataches(..., batch_format="pandas")
```
At a high level we read, write, read. On a lower-level, we convert arrow
blocks -> pandas -> arrow blocks -> pandas. We have connectors and
registered extension types in `python/ray/air/util/tensor_extensions/`,
however we special case handle tensor types by converting them to
`TensorArrays`
[here](https://github.com/iamjustinhsu/ray/blob/1f7dcec413bf9aba3ac39c0a14d7d4b734a1939f/python/ray/data/_internal/pandas_block.py#L238)
when we convert pandas -> arrow. During this process, however, pyarrow
will store metadata about the pandas block, which will look something
like this:
```json
{
    "name": "feature1",
    "field_name": "feature1",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8, 2), dtype=<U38)",
    "metadata": null
},
{
    "name": "feature2",
    "field_name": "feature2",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8,), dtype=float32)",
    "metadata": null
}
```
For the most part this is fine, however, when converting _back_ to
pandas, arrow will first attempt to search through the
metadata("numpy_type") to restore the schema. This can be troublesome
because pandas/numpy doesn't know how to handle those custom types.

In pyarrow==14.0.0, this is an issue, because it surrenders the special
casing to numpy/pandas
in pyarrow==21.0.0, it's smarter and DOES handle that (I tested this) 

### Solution
To handle older pyarrow versions, we can do `ignore_metadata=True`

## Related issue number

<!-- For example: "Closes ray-project#1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: iamjustinhsu <[email protected]>
Signed-off-by: zac <[email protected]>
dstrodtman pushed a commit to dstrodtman/ray that referenced this pull request Oct 6, 2025
<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?
Consider the following the code
```python
import ray
# Read File (1)
source_path = "file_that_contains_tensor_strings.parquet"
ds = ray.data.read_parquet(source_path)
# Write File (2)
dest_path = "/tmp"
ds.map_batches(..., batch_format="pandas").write_parquet(dest_path)

# Read File Again (3)
new_ds = ray.data.read_parquet(dest_path).map_bataches(..., batch_format="pandas")
```
At a high level we read, write, read. On a lower-level, we convert arrow
blocks -> pandas -> arrow blocks -> pandas. We have connectors and
registered extension types in `python/ray/air/util/tensor_extensions/`,
however we special case handle tensor types by converting them to
`TensorArrays`
[here](https://github.com/iamjustinhsu/ray/blob/1f7dcec413bf9aba3ac39c0a14d7d4b734a1939f/python/ray/data/_internal/pandas_block.py#L238)
when we convert pandas -> arrow. During this process, however, pyarrow
will store metadata about the pandas block, which will look something
like this:
```json
{
    "name": "feature1",
    "field_name": "feature1",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8, 2), dtype=<U38)",
    "metadata": null
},
{
    "name": "feature2",
    "field_name": "feature2",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8,), dtype=float32)",
    "metadata": null
}
```
For the most part this is fine, however, when converting _back_ to
pandas, arrow will first attempt to search through the
metadata("numpy_type") to restore the schema. This can be troublesome
because pandas/numpy doesn't know how to handle those custom types.

In pyarrow==14.0.0, this is an issue, because it surrenders the special
casing to numpy/pandas
in pyarrow==21.0.0, it's smarter and DOES handle that (I tested this)

### Solution
To handle older pyarrow versions, we can do `ignore_metadata=True`

## Related issue number

<!-- For example: "Closes ray-project#1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: iamjustinhsu <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?
Consider the following the code
```python
import ray
# Read File (1)
source_path = "file_that_contains_tensor_strings.parquet"
ds = ray.data.read_parquet(source_path)
# Write File (2)
dest_path = "/tmp"
ds.map_batches(..., batch_format="pandas").write_parquet(dest_path)

# Read File Again (3)
new_ds = ray.data.read_parquet(dest_path).map_bataches(..., batch_format="pandas")
```
At a high level we read, write, read. On a lower-level, we convert arrow
blocks -> pandas -> arrow blocks -> pandas. We have connectors and
registered extension types in `python/ray/air/util/tensor_extensions/`,
however we special case handle tensor types by converting them to
`TensorArrays`
[here](https://github.com/iamjustinhsu/ray/blob/1f7dcec413bf9aba3ac39c0a14d7d4b734a1939f/python/ray/data/_internal/pandas_block.py#L238)
when we convert pandas -> arrow. During this process, however, pyarrow
will store metadata about the pandas block, which will look something
like this:
```json
{
    "name": "feature1",
    "field_name": "feature1",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8, 2), dtype=<U38)",
    "metadata": null
},
{
    "name": "feature2",
    "field_name": "feature2",
    "pandas_type": "object",
    "numpy_type": "numpy.ndarray(shape=(8,), dtype=float32)",
    "metadata": null
}
```
For the most part this is fine, however, when converting _back_ to
pandas, arrow will first attempt to search through the
metadata("numpy_type") to restore the schema. This can be troublesome
because pandas/numpy doesn't know how to handle those custom types.

In pyarrow==14.0.0, this is an issue, because it surrenders the special
casing to numpy/pandas
in pyarrow==21.0.0, it's smarter and DOES handle that (I tested this) 

### Solution
To handle older pyarrow versions, we can do `ignore_metadata=True`

## Related issue number

<!-- For example: "Closes ray-project#1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: iamjustinhsu <[email protected]>
iamjustinhsu pushed a commit that referenced this pull request Oct 30, 2026
## 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]>
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