Repository navigation
GH-49384: [Python] Respect copy=True in ChunkedArray.__array__ - #50561
jingyi-zhao-01 wants to merge 6 commits into
Conversation
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!
AlenkaF
left a comment
There was a problem hiding this comment.
Adding additional comments. I would be happy to hear opinions before changes are added in the PR.
There was a problem hiding this comment.
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).
|
|
||
| def __array__(self, dtype=None, copy=None): | ||
| self._assert_cpu() | ||
| if copy is False: |
There was a problem hiding this comment.
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.
|
Ah, there is a connected issue: #38789 As mentioned, am interested in your thoughts first before reviewing new changes. |
let me take a look at that related issue |
|
i think we could first check whether zero-copy is possible, instead of rejecting zero_copy_only=True and copy=False immediately. |
|
Yes, I would be in favor of doing this in existing ChunkedArray conversion path not passing single chunks to Array. |
|
i have updated the change, maybe we can close that relevant issue as well ? |
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.