Skip to content

Commit

Permalink
GH-34787: [Python] Accept zero_copy_only=False for ChunkedArray.to_nu…
Browse files Browse the repository at this point in the history
…mpy (#35582)

### Rationale for this change

Addresses GH-34787.

### What changes are included in this PR?

I think this proposes the minimal viable changes to have the same signature as proposed in GH-34787.

### Are these changes tested?

Yes, for now, a single simple test has been introduced to test the correct behavior of `ChunkedArray.to_numpy` with respect to the addition of `zero_copy_only`.

* Closes: #34787

Lead-authored-by: Julien Jerphanion <git@jjerphan.xyz>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
jjerphan and jorisvandenbossche committed Jul 10, 2023
1 parent 8eacd8c commit a485bb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,16 @@ cdef class ChunkedArray(_PandasConvertible):
def _to_pandas(self, options, types_mapper=None, **kwargs):
return _array_like_to_pandas(self, options, types_mapper=types_mapper)

def to_numpy(self):
def to_numpy(self, zero_copy_only=False):
"""
Return a NumPy copy of this array (experimental).
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.
Returns
-------
array : numpy.ndarray
Expand All @@ -475,6 +481,10 @@ cdef class ChunkedArray(_PandasConvertible):
>>> n_legs.to_numpy()
array([ 2, 2, 4, 4, 5, 100])
"""
if zero_copy_only:
raise ValueError(
"zero_copy_only must be False for pyarrow.ChunkedArray.to_numpy"
)
cdef:
PyObject* out
PandasOptions c_options
Expand Down
15 changes: 15 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ def test_to_numpy_zero_copy():
np.testing.assert_array_equal(np_arr, expected)


def test_chunked_array_to_numpy_zero_copy():
elements = [[2, 2, 4], [4, 5, 100]]

chunked_arr = pa.chunked_array(elements)

msg = "zero_copy_only must be False for pyarrow.ChunkedArray.to_numpy"

with pytest.raises(ValueError, match=msg):
chunked_arr.to_numpy(zero_copy_only=True)

np_arr = chunked_arr.to_numpy()
expected = [2, 2, 4, 4, 5, 100]
np.testing.assert_array_equal(np_arr, expected)


def test_to_numpy_unsupported_types():
# ARROW-2871: Some primitive types are not yet supported in to_numpy
bool_arr = pa.array([True, False, True])
Expand Down

0 comments on commit a485bb7

Please sign in to comment.