Repository navigation
GH-49384: [Python] Respect copy=True in ChunkedArray.__array__ - #50561
jingyi-zhao-01 wants to merge 3 commits into
Conversation
| # '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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
uros-b
left a comment
There was a problem hiding this comment.
I left a few comments, PTAL. Otherwise, nice fix - thank you @jingyi-zhao-01!
Rationale for this change
ChunkedArray.__array__(copy=True)should respect NumPy's copy request. For a single-chunkChunkedArray, the current path can return a zero-copy read-only NumPy view instead.Closes #49384.
What changes are included in this PR?
ChunkedArray.__array__(copy=True)return a real NumPy copy.Are these changes tested?
Yes:
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 fromcopy=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.