sameer727 opened a new pull request, #55727:
URL: https://github.com/apache/arrow/pull/55727
### Rationale for this change
`Table.to_pandas(zero_copy_only=True)` always fails with `ArrowInvalid:
Cannot do zero copy conversion into multi-column DataFrame block`, even for a
single numeric column (#39194). In the consolidated-block path
(`split_blocks=False`), `allow_zero_copy_blocks` is never enabled, so the
zero-copy check in the pandas conversion always raises.
### What changes are included in this PR?
When `zero_copy_only=True` is requested for a Table or RecordBatch
conversion, the columns are converted as separate pandas blocks (as if
`split_blocks=True` had been given): a multi-column block cannot reference the
original Arrow memory, while per-column blocks can.
Conversions that genuinely need to copy still raise `ArrowInvalid`, e.g.
columns with nulls (`Needed to copy 1 chunks with 1 nulls, but zero_copy_only
was True`), `strings_to_categorical=True` / `categories` (`Need to dictionary
encode a column, but only zero-copy conversions allowed`).
The `zero_copy_only` parameter docstring was updated to describe this, and
regression tests were added to `TestZeroCopyConversion`.
### Are these changes tested?
Yes, locally with pyarrow 24.0.0 + pandas 3.0.2 (Windows, Python 3.13): the
change is pure Python (`pandas_compat.py`), so the modified
`table_to_dataframe` was exercised directly against the installed wheel:
- `pa.table({'a': [1.0, 2.0, 3.0], 'b': [4.0, 5.0,
6.0]}).to_pandas(zero_copy_only=True)` now succeeds (previously raised);
- single-column tables, tables with pandas-metadata indexes, RecordBatches,
and mixed numeric/string tables succeed;
- copies still raise: int column with nulls, `strings_to_categorical=True`,
`categories=[...]`;
- the default (`zero_copy_only=False`) path and the Series/ChunkedArray path
are unchanged; the resulting block layout is per-column only when
`zero_copy_only=True` is requested;
- `pd.read_parquet` / `pyarrow.dataset` read paths behave as before.
The three new tests in `python/pyarrow/tests/test_pandas.py` (success,
copy-needed failure, strings_to_categorical failure) were run locally against
the wheel (15 passed in the zero-copy test class).
### Are there any user-facing changes?
`Table.to_pandas(zero_copy_only=True)` /
`RecordBatch.to_pandas(zero_copy_only=True)` now succeed instead of always
raising. With `zero_copy_only=True` the resulting DataFrame uses one pandas
block per column (this is what makes zero-copy possible; previously
`split_blocks=True` had to be passed manually for this).
### Was AI used for this PR?
In accordance to the [AI generation
guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
please disclose below whether and how AI was used in this PR.
**PR code and description written by:**
- [ ] Human
- [x] AI
**Reviewed before submission by:**
- [ ] Human
- [x] AI
- [ ] Not reviewed
This PR was prepared with an AI coding agent (ZCode) at the direction of the
submitter. The C++ conversion path (`arrow_to_pandas.cc`) was traced to
identify the root cause (`allow_zero_copy_blocks` only being enabled when
`split_blocks=True`), the Python-side fix was verified by execution against the
installed pyarrow 24.0.0 wheel (details above), and the change is confined to
`pandas_compat.py` plus docs/tests.
* GitHub Issue: #39194
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]