Skip to content

Commit 8e8265b

Browse files
jorisvandenbosschemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#64035: BUG: don't mark pyarrow-string to numpy array as read-only unnecessarily
1 parent a80a89b commit 8e8265b

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎doc/source/whatsnew/v3.0.1.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression in :meth:`DataFrame.groupby` and :meth:`Series.groupby` when grouping on categorical data with NA values, ``observed=False``, and ``dropna=True`` (:issue:`52445`)
17+
- Fixed regression when calling ``numpy.random``'s ``permutation()`` on a (pyarrow-backed) string :class:`Series` (:issue:`63935`)
1718

1819
.. ---------------------------------------------------------------------------
1920
.. _whatsnew_301.bugs:

‎pandas/core/dtypes/astype.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool:
277277
return False
278278

279279
elif is_string_dtype(dtype) and is_string_dtype(new_dtype):
280+
from pandas.core.arrays.string_ import StringDtype
281+
282+
if (
283+
isinstance(dtype, StringDtype)
284+
and dtype.storage == "pyarrow"
285+
and new_dtype == "object"
286+
):
287+
# for conversion of pyarrow array to numpy object array -> always a copy
288+
return False
280289
# Potentially! a view when converting from object to string
281290
return True
282291

‎pandas/tests/arrays/string_/test_string.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,14 @@ def test_numpy_array_ufunc(dtype, box):
610610
expected = pd.Series(["aa", "bbbb", "cccccc"])
611611

612612
tm.assert_equal(result, expected)
613+
614+
615+
@pytest.mark.parametrize("box", [pd.Series, pd.array])
616+
def test_numpy_random_permute(dtype, box):
617+
# https://github.com/pandas-dev/pandas/issues/63935
618+
arr = box(["a", "bb", "ccc"], dtype=dtype)
619+
620+
rng = np.random.default_rng(2)
621+
result = rng.permutation(arr)
622+
assert isinstance(result, np.ndarray)
623+
assert sorted(result.tolist()) == ["a", "bb", "ccc"]

‎pandas/tests/copy_view/test_array.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ def test_dataframe_array_string_dtype():
180180
assert arr.flags.writeable is False
181181

182182

183+
def test_series_array_string_dtype(any_string_dtype):
184+
ser = Series(["a", "b"], dtype=any_string_dtype)
185+
arr = np.asarray(ser)
186+
if any_string_dtype == "string" and any_string_dtype.storage == "pyarrow":
187+
# for pyarrow strings, the numpy arrays is not a view, so also does
188+
# not need to be read-only (https://github.com/pandas-dev/pandas/pull/64035)
189+
assert not np.shares_memory(arr, get_array(ser))
190+
assert arr.flags.writeable is True
191+
else:
192+
assert np.shares_memory(arr, get_array(ser))
193+
assert arr.flags.writeable is False
194+
195+
183196
def test_dataframe_multiple_numpy_dtypes():
184197
df = DataFrame({"a": [1, 2, 3], "b": 1.5})
185198
arr = np.asarray(df)

0 commit comments

Comments
 (0)