Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Bug fixes
- Fixed a bug in :func:`col` where unary operators (``-``, ``+``, ``abs``) were not supported (:issue:`63939`)
- Fixed a bug in the :func:`comparison_op` raising a ``TypeError`` for zerodim
subclasses of ``np.ndarray`` (:issue:`63205`)
- Fixed a bug in :func:`numpy.random.Generator.permutation` would fail with ``ValueError`` when called on :class:`Series` with PyArrow-backed dtypes (:issue:`63935`)

.. ---------------------------------------------------------------------------
.. _whatsnew_301.contributors:
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,11 +854,10 @@ def __array__(
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
elif copy is None:
# `to_numpy(copy=False)` has the meaning of NumPy `copy=None`.
copy = False

return self.to_numpy(dtype=dtype, copy=copy)
result = self.to_numpy(dtype=dtype, copy=copy if copy else False)
if copy is None and not result.flags.writeable:
result = result.copy()
return result

def __invert__(self) -> Self:
# This is a bit wise op for integer types
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def __array__(

if copy is True:
return arr
if copy is False or astype_is_view(values.dtype, arr.dtype):
if np.shares_memory(arr, values):
arr = arr.view()
arr.flags.writeable = False
return arr
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/series/test_arrow_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ctypes

import numpy as np
import pytest

import pandas.util._test_decorators as td
Expand Down Expand Up @@ -115,3 +116,20 @@ def test_dataframe_from_arrow():
TypeError, match="Expected an Arrow-compatible array-like object"
):
pd.Series.from_arrow([1, 2, 3])


@pytest.mark.parametrize(
"dtype,data",
[
("string[pyarrow]", ["foo", "bar", "baz"]),
("int64[pyarrow]", [1, 2, 3]),
("float64[pyarrow]", [1.0, 2.0, 3.0]),
],
)
def test_numpy_permutation_pyarrow_dtypes(dtype, data):
# GH#63935
rng = np.random.default_rng(42)
s = pd.Series(data, dtype=dtype)
result = rng.permutation(s)
assert len(result) == len(data)
assert set(result) == set(data)
Loading