Skip to content

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

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

jingyi-zhao-01 wants to merge 3 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
# 'copy' can further be ignored because to_numpy() already returns a copy
values = self.to_numpy()
if copy is True:
return np.array(values, dtype=dtype, copy=True)

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.

Please note: for multi-chunk or has-nulls arrays, to_numpy() already returns a fresh copy, then np.array(values, copy=True) copies it again unnecessarily. For example, the sibling Array.__array__ (in array.pxi) avoids this by guarding the extra copy:

if copy is True and is_numeric(self.type.id) and self.null_count == 0:
      return np.array(values, dtype=dtype, copy=True)

however, doing that here might be a bit involved - in that case, it's probably fine to leave as-is.

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 think this is a valid comment and should be addressed. I would also like to see the docstring for to_numpy() updated to reflect single chunked arrays do not get copied and there is no way (currently) to force a copy.

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!

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