Skip to content

GH-34787: [Python] Accept zero_copy_only=False for ChunkedArray.to_numpy - #35582

Merged
jorisvandenbossche merged 4 commits into
apache:mainfrom
jjerphan:python/pyarrow.ChunkedArray.to_numpy_zero_copy_only=False
Jul 10, 2023
Merged

jorisvandenbossche merged 4 commits into
apache:mainfrom
jjerphan:python/pyarrow.ChunkedArray.to_numpy_zero_copy_only=False

Conversation

@jjerphan

@jjerphan jjerphan commented May 13, 2023 •

Copy link
Copy Markdown
Contributor

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.

Comments

Looking at the signature of pa.Array.to_numpy:

arrow/python/pyarrow/array.pxi

Lines 1486 to 1512 in b211c18

def to_numpy(self, zero_copy_only=True, writable=False):
"""
Return a NumPy view or copy of this array (experimental).
By default, tries to return a view of this array. This is only
supported for primitive arrays with the same memory layout as NumPy
(i.e. integers, floating point, ..) and without any nulls.
For the extension arrays, this method simply delegates to the
underlying storage array.
Parameters
----------
zero_copy_only : bool, default True
If True, an exception will be raised if the conversion to a numpy
array would require copying the underlying data (e.g. in presence
of nulls, or for non-primitive types).
writable : bool, default False
For numpy arrays created with zero copy (view on the Arrow data),
the resulting array is not writable (Arrow data is immutable).
By setting this to True, a copy of the array is made to ensure
it is writable.
Returns
-------
array : numpy.ndarray
"""

I think writable can be added as a default-valued parameter to pa.ChunkedArray.to_numpy.

What do you think?

@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #34787 has been automatically assigned in GitHub to PR creator.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #34787 has been automatically assigned in GitHub to PR creator.

@jjerphan
jjerphan marked this pull request as ready for review May 13, 2023 15:24
@jjerphan
jjerphan requested a review from AlenkaF as a code owner May 13, 2023 15:24
@AlenkaF

AlenkaF commented May 16, 2023

Copy link
Copy Markdown
Member

Thank you for the contribution @jjerphan!

I think writable can be added as a default-valued parameter to pa.ChunkedArray.to_numpy.

Yes, I think that would be useful to add 👍

@jjerphan

Copy link
Copy Markdown
Contributor Author

Thank you for the contribution @jjerphan!

I think writable can be added as a default-valued parameter to pa.ChunkedArray.to_numpy.

Yes, I think that would be useful to add +1

Thanks for the feedback, @AlenkaF!

Should it be done in another PR?

@AlenkaF

AlenkaF commented May 16, 2023

Copy link
Copy Markdown
Member

Should it be done in another PR?

I would personally add it in this PR. WIll change the title to include both changes.

Comment thread python/pyarrow/table.pxi Outdated
PandasOptions c_options
object values

if zero_copy_only:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, how about putting the if/raise statement before the cdef?

@jjerphan
jjerphan marked this pull request as draft June 7, 2023 07:23

@jorisvandenbossche jorisvandenbossche left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! It's a goode idea to add the writeable keyword here as well, but I think also fine to leave that for another PR.

Comment thread python/pyarrow/tests/test_array.py Outdated
Comment on lines +183 to +188
chunked_arr = None
import gc
gc.collect()

# Ensure base is still valid
assert np_arr.base is not None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this part is needed here, as this is not actually zero copy (the base is still set, because we concatenate on the arrow side, and then still keep this concatenated buffer alive through a capsule as the numpy array's base).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so we do not want to test the presence of np_arr.base here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is needed, it's in any case not specific to this test

@jjerphan

Copy link
Copy Markdown
Contributor Author

Thank you for the review, @jorisvandenbossche! I will come back to this PR when I get some bandwidth.

jjerphan added 2 commits July 9, 2023 11:48
Co-authored-by: Joris Van den Bossche <[email protected]>

Signed-off-by: Julien Jerphanion <[email protected]>
Comment thread python/pyarrow/tests/test_array.py Resolved
@jorisvandenbossche
jorisvandenbossche marked this pull request as ready for review July 10, 2023 08:11
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #34787 has been automatically assigned in GitHub to PR creator.

@jorisvandenbossche
jorisvandenbossche merged commit a485bb7 into apache:main Jul 10, 2023
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #34787 has been automatically assigned in GitHub to PR creator.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #34787 has been automatically assigned in GitHub to PR creator.

@jorisvandenbossche

Copy link
Copy Markdown
Member

Thanks @jjerphan! Already merging this, so it can be included in pyarrow 13.0.0. But a follow-up PR to add writeable as well is certainly welcome.

@jjerphan
jjerphan deleted the python/pyarrow.ChunkedArray.to_numpy_zero_copy_only=False branch July 10, 2023 08:45
@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit a485bb7.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about possible false positives for unstable benchmarks that are known to sometimes produce them.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] Accept keywords in ChunkedArray.to_numpy() for compatibility with Array.to_numpy()

4 participants