Skip to content

[Python][Docs] ChunkedArray.to_numpy() sets the OWNDATA and WRITEABLE flags to False #38789

In progress

Description

@alippai

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

pa.chunked_array([[1.1]]).to_numpy(zero_copy_only=False).flags

Results in:

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

The docs say: "Introduced for signature consistence with pyarrow.Array.to_numpy. This must be False here since NumPy arrays’ buffer must be contiguous."

As we are returning a copy, OWNDATA and WRITEABLE should be True.

PyArrow version: 14.0.1
Numpy version: 1.26.0

Component(s)

Python

Activity

  1. alippai commented on Nov 23, 2023

    @alippai
    ContributorAuthor
  2. alippai commented on Feb 6, 2024

    @alippai
    ContributorAuthor

    Gentle ping @AlenkaF @jorisvandenbossche
    I'm not sure what to look for, but if you point me into the right direction I could possibly help.

  3. AlenkaF commented on Feb 7, 2024

    @AlenkaF
    Member

    Thank you for opening an issue and sorry for such a late reply!

    I agree, the documentation for ChunkedArray.to_numpy is a bit misleading. But the behaviour should actually be correct!

    The example you are using in the issue description is a special case as the chunked array consists only of one chunk and can therefore be converted to numpy zero copy:

    In [1]: import pyarrow as pa
    
    In [2]: arr = pa.chunked_array([[1.1]])
    
    In [3]: arr.to_numpy(zero_copy_only=False).base.base
    Out[3]: 
    <pyarrow.lib.ChunkedArray object at 0x105329170>
    [
      [
        1.1
      ]
    ]
    
    In [4]: arr
    Out[4]: 
    <pyarrow.lib.ChunkedArray object at 0x105329170>
    [
      [
        1.1
      ]
    ]

    which means the code in PyArrow that checks for zero_copy_only=False should be changed to allow zero copy in case of a single chunk together with the docs for zero_copy_only attribute:

    if zero_copy_only:
    raise ValueError(
    "zero_copy_only must be False for pyarrow.ChunkedArray.to_numpy"
    )

    Parameters
    ----------
    zero_copy_only : bool, default False
    Introduced for signature consistence with pyarrow.Array.to_numpy.
    This must be False here since NumPy arrays' buffer must be contiguous.

    But the flags OWNDATA : False and WRITEABLE : False are correct.

    Now if the chunked array has more than one chunk, the WRITEABLE flag is different but OWNDATA is still false as Arrow is holding on to the memory of the Buffer created with the copy:

    In [5]: arr = pa.chunked_array([[1.1], [2.2]])
    
    In [6]: arr.to_numpy(zero_copy_only=False).base.base
    Out[6]: <capsule object "arrow::Buffer" at 0x10a017b10>

    To sum up, what I think we should do is:

    Your contribution would be more than welcome, thanks!

  4. changed the title [-]ChunkedArray.to_numpy() sets the OWNDATA and WRITEABLE flags to False[/-] [+][Python][Docs] ChunkedArray.to_numpy() sets the OWNDATA and WRITEABLE flags to False[/+] on Feb 7, 2024
  5. sameer727 commented on Nov 27, 2026

    @sameer727
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions