Skip to content

GH-49384: [Python] Respect copy=True in ChunkedArray.__array__ - #50561

Open
jingyi-zhao-01 wants to merge 6 commits into
apache:mainfrom
jingyi-zhao-01:GH-49384-copy-true-chunkedarray-final
Open

jingyi-zhao-01 wants to merge 6 commits into
apache:mainfrom
jingyi-zhao-01:GH-49384-copy-true-chunkedarray-final

Conversation

@jingyi-zhao-01

@jingyi-zhao-01 jingyi-zhao-01 commented Jul 20, 2026 •

Copy link
Copy Markdown

Rationale for this change

ChunkedArray.__array__(copy=True) should respect NumPy's copy request. For a single-chunk ChunkedArray, the current path can return a zero-copy read-only NumPy view instead.

Closes #49384.

What changes are included in this PR?

  • Make ChunkedArray.__array__(copy=True) return a real NumPy copy.
  • Add a regression test for the single-chunk case.

Are these changes tested?

Yes:

python -m pytest python/pyarrow/tests/test_table.py -q

Are there any user-facing changes?

Yes. np.array(pa.chunked_array([[...]]), copy=True) now returns a writable copied NumPy array, matching the behavior users expect from copy=True.

AI assistance

I used an AI coding assistant while preparing this patch. I reviewed the change myself, kept it scoped to ChunkedArray.__array__, and verified it with the focused PyArrow table tests above.

Comment thread python/pyarrow/table.pxi Resolved
Comment thread python/pyarrow/tests/test_table.py Resolved

@uros-b uros-b left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left a few comments, PTAL. Otherwise, nice fix - thank you @jingyi-zhao-01!

@AlenkaF AlenkaF left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Adding additional comments. I would be happy to hear opinions before changes are added in the PR.

Comment thread python/pyarrow/table.pxi Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This line can be removed now or updated so that this holds only for ChunkedArrays with more than 1 chunk (if we delegate single chunks to Array method).

Comment thread python/pyarrow/table.pxi

def __array__(self, dtype=None, copy=None):
self._assert_cpu()
if copy is False:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it make sense to delegate single chunks to the Array method, like so?

    if self.num_chunks == 1:
        return self.chunk(0).__array__(dtype, copy)

This way we would not need to guard for double copy and also we would be able to pass copy=False flag for single chunks (primitive) without nulls while it currently errors with ValueError.

@AlenkaF

AlenkaF commented Nov 30, 2026

Copy link
Copy Markdown
Member

Ah, there is a connected issue: #38789
We should fix both with this PR. Maybe looking at the Array implementations and using additional parameters if needed. Branching off single chunked arrays to Array implementation might be a wrong decision here.

As mentioned, am interested in your thoughts first before reviewing new changes.

@jingyi-zhao-01

Copy link
Copy Markdown
Author

Ah, there is a connected issue: #38789 We should fix both with this PR. Maybe looking at the Array implementations and using additional parameters if needed. Branching off single chunked arrays to Array implementation might be a wrong decision here.

As mentioned, am interested in your thoughts first before reviewing new changes.

let me take a look at that related issue

@jingyi-zhao-01

Copy link
Copy Markdown
Author

i think we could first check whether zero-copy is possible, instead of rejecting zero_copy_only=True and copy=False immediately.
If it’s possible, we return a view. if a copy is needed, we raise an error. could we do this in the existing ChunkedArray conversion path? I’m not sure whether passing single chunks to Array would change the behavior for some types. What do you think?
@AlenkaF

@AlenkaF

AlenkaF commented Dec 1, 2026

Copy link
Copy Markdown
Member

Yes, I would be in favor of doing this in existing ChunkedArray conversion path not passing single chunks to Array.

@jingyi-zhao-01

Copy link
Copy Markdown
Author

i have updated the change, maybe we can close that relevant issue as well ?
@AlenkaF

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.

[Python] Single chunk ChunkedArray doesn't correctly respect copy in __array__method

4 participants