Skip to content

Commit 81fea58

Browse files
authored
API CoW: Return read_only NumPy array from ravel (#52060)
1 parent 8c7b8a4 commit 81fea58

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/series.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,10 @@ def ravel(self, order: str = "C") -> ArrayLike:
768768
--------
769769
numpy.ndarray.ravel : Return a flattened array.
770770
"""
771-
return self._values.ravel(order=order)
771+
arr = self._values.ravel(order=order)
772+
if isinstance(arr, np.ndarray) and using_copy_on_write():
773+
arr.flags.writeable = False
774+
return arr
772775

773776
def __len__(self) -> int:
774777
"""

pandas/tests/copy_view/test_array.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,12 @@ def test_series_to_numpy(using_copy_on_write):
110110
arr = ser.to_numpy(dtype="float64")
111111
assert not np.shares_memory(arr, get_array(ser, "name"))
112112
assert arr.flags.writeable is True
113+
114+
115+
@pytest.mark.parametrize("order", ["F", "C"])
116+
def test_ravel_read_only(using_copy_on_write, order):
117+
ser = Series([1, 2, 3])
118+
arr = ser.ravel(order=order)
119+
if using_copy_on_write:
120+
assert arr.flags.writeable is False
121+
assert np.shares_memory(get_array(ser), arr)

0 commit comments

Comments
 (0)