Skip to content

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

Description

@BenLewis-Seequent

Describe the bug, including details regarding any error messages, version, and platform.

The expectation is that if copy=True is passed into the __array__ method, the returned numpy array should be writable. This is true if the chunked array contains more than one chunk, but if the array contains a single chunk the returned array is not writable.

The below code:

pyarrow.chunked_array([[2, 3]]).__array__(copy=True).flags

Returns:

C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : False
ALIGNED : True WRITEBACKIFCOPY : False

I originally discovered this when numpy.array(chunked_array) returned a read-only array.

Environment:
Python: 3.13.0
OS: Linux
PyArrow version: 23.0.1

Component(s)

Python

Activity

  1. xovishnukosuri commented on Mar 14, 2026

    @xovishnukosuri

    take

  2. AlenkaF commented on Mar 26, 2026

    @AlenkaF
    Member

    Thank you for opening the issue, looks like this indeed is a bug.
    After doing some investigating, it looks like this happens when chunked array has only one chunk and can therefore be zero-copied (no missing values, etc.):

    >>> np.array(pa.chunked_array([[1, 2, 3]]), copy=True).flags.writeable
    False
    >>> np.array(pa.chunked_array([[1, 2], [3]]), copy=True).flags.writeable
    True

    The comment in the code:

    # 'copy' can further be ignored because to_numpy() already returns a copy

    is valid for chunked arrays with number of chunks > 0. Otherwise the code needs to take into account the copy flag.

    I think the PR for this should be quite straightforward, contributions are most welcome!

  3. changed the title [-]Single chunk ChunkedArray doesn't correctly respect copy in __array__method[/-] [+][Python] Single chunk ChunkedArray doesn't correctly respect copy in __array__method[/+] on Mar 27, 2026
  4. jingyi-zhao-01 commented on Jul 18, 2026

    @jingyi-zhao-01

    I can still reproduce this on current Arrow main / local PyArrow dev build:

    • pyarrow 26.0.0.dev3+gb03b4c539
    • numpy 2.5.1

    Minimal repro:

    import numpy as np
    import pyarrow as pa
    
    for arr in [pa.chunked_array([[2, 3]]), pa.chunked_array([[2], [3]])]:
        out = np.array(arr, copy=True)
        print(arr.num_chunks, out.flags.owndata, out.flags.writeable)
        try:
            out[0] = 99
            print("mutation ok", out.tolist())
        except Exception as exc:
            print(type(exc).__name__, exc)

    Observed output:

    1 False False
    ValueError assignment destination is read-only
    2 False True
    mutation ok [99, 3]
    

    A PR has been opened to address this issue

  5. raulcd commented on Jul 20, 2026

    @raulcd
    Member

    This seems to have been closed by mistake.

  6. reopened this on Jul 20, 2026
  7. added a commit that references this issue on Nov 12, 2026
  8. added
    Status: confirmed
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Component: PythonStatus: confirmedType: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions