Skip to content

Commit 77627e3

Browse files
Backport PR pandas-dev#52060 on branch 2.0.x (API CoW: Return read_only NumPy array from ravel) (pandas-dev#52273)
Backport PR pandas-dev#52060: API CoW: Return read_only NumPy array from ravel Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 4c2c3f8 commit 77627e3

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/series.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,10 @@ def ravel(self, order: str = "C") -> ArrayLike:
772772
--------
773773
numpy.ndarray.ravel : Return a flattened array.
774774
"""
775-
return self._values.ravel(order=order)
775+
arr = self._values.ravel(order=order)
776+
if isinstance(arr, np.ndarray) and using_copy_on_write():
777+
arr.flags.writeable = False
778+
return arr
776779

777780
def __len__(self) -> int:
778781
"""

pandas/tests/copy_view/test_array.py

+9
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)