Skip to content

GH-39194: [Python] Make Table.to_pandas(zero_copy_only=True) succeed when possible - #55727

Open
sameer727 wants to merge 3 commits into
apache:mainfrom
sameer727:fix-zero-copy-to-pandas-39194
Open

sameer727 wants to merge 3 commits into
apache:mainfrom
sameer727:fix-zero-copy-to-pandas-39194

Conversation

@sameer727

@sameer727 sameer727 commented Nov 22, 2026 •

Copy link
Copy Markdown

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, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • 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.

…eld kernel

The struct_field kernel accepts a FieldRef in StructFieldOptions, but
the compute documentation only described the sequence-of-indices form.

Document that the reference can be a FieldPath of child indices, a
field name, or a nested sequence of these, with field-name examples
added alongside the existing index examples. Also fix the union
example, which referred to a child array "a" that does not exist in
the sparse_union type used there.
…tioned dataset

When writing a partitioned dataset with pq.write_to_dataset, the
partition columns are not stored in the data files (they are encoded
in the directory names). The collected file metadata therefore has a
schema without those columns, and calling pq.write_metadata with the
full table schema for the _metadata file fails with
"AppendRowGroups requires equal schemas".

Add an example to the "Writing _metadata and _common_metadata files"
section showing how to write the metadata files for a partitioned
dataset: the _common_metadata file gets the full dataset schema,
while the partition columns need to be removed from the schema
passed for the _metadata file.

The example was verified with pyarrow 24.
…cceed when possible

Zero-copy conversion of a Table or RecordBatch into a pandas DataFrame
was always failing with "Cannot do zero copy conversion into
multi-column DataFrame block": in the consolidated-block path
(split_blocks=False), allow_zero_copy_blocks is never enabled, so the
zero-copy check raises even for a single column.

When zero_copy_only=True is requested, convert the columns 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, or strings_to_categorical /
categories).

Documented the behavior in the zero_copy_only parameter description and
added regression tests.

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants