Skip to content

Commit ae81b62

Browse files
jorisvandenbosschemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#60191: TST: add extra test case for np.array(obj, copy=False) read-only behaviour
1 parent b5d0615 commit ae81b62

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

pandas/core/generic.py

+6
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,12 @@ def empty(self) -> bool_t:
21492149
def __array__(
21502150
self, dtype: npt.DTypeLike | None = None, copy: bool_t | None = None
21512151
) -> np.ndarray:
2152+
if copy is False and not self._mgr.is_single_block and not self.empty:
2153+
# check this manually, otherwise ._values will already return a copy
2154+
# and np.array(values, copy=False) will not raise an error
2155+
raise ValueError(
2156+
"Unable to avoid copy while creating an array as requested."
2157+
)
21522158
values = self._values
21532159
if copy is None:
21542160
# Note: branch avoids `copy=None` for NumPy 1.x support

pandas/tests/copy_view/test_array.py

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.compat.numpy import np_version_gt2
5+
46
from pandas import (
57
DataFrame,
68
Series,
@@ -15,8 +17,12 @@
1517

1618
@pytest.mark.parametrize(
1719
"method",
18-
[lambda ser: ser.values, lambda ser: np.asarray(ser)],
19-
ids=["values", "asarray"],
20+
[
21+
lambda ser: ser.values,
22+
lambda ser: np.asarray(ser),
23+
lambda ser: np.array(ser, copy=False),
24+
],
25+
ids=["values", "asarray", "array"],
2026
)
2127
def test_series_values(using_copy_on_write, method):
2228
ser = Series([1, 2, 3], name="name")
@@ -45,8 +51,12 @@ def test_series_values(using_copy_on_write, method):
4551

4652
@pytest.mark.parametrize(
4753
"method",
48-
[lambda df: df.values, lambda df: np.asarray(df)],
49-
ids=["values", "asarray"],
54+
[
55+
lambda df: df.values,
56+
lambda df: np.asarray(df),
57+
lambda ser: np.array(ser, copy=False),
58+
],
59+
ids=["values", "asarray", "array"],
5060
)
5161
def test_dataframe_values(using_copy_on_write, using_array_manager, method):
5262
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
@@ -100,7 +110,7 @@ def test_series_to_numpy(using_copy_on_write):
100110
arr[0] = 0
101111
assert ser.iloc[0] == 0
102112

103-
# specify copy=False gives a writeable array
113+
# specify copy=True gives a writeable array
104114
ser = Series([1, 2, 3], name="name")
105115
arr = ser.to_numpy(copy=True)
106116
assert not np.shares_memory(arr, get_array(ser, "name"))
@@ -174,6 +184,23 @@ def test_dataframe_multiple_numpy_dtypes():
174184
assert not np.shares_memory(arr, get_array(df, "a"))
175185
assert arr.flags.writeable is True
176186

187+
if np_version_gt2:
188+
# copy=False semantics are only supported in NumPy>=2.
189+
190+
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
191+
arr = np.array(df, copy=False)
192+
193+
arr = np.array(df, copy=True)
194+
assert arr.flags.writeable is True
195+
196+
197+
def test_dataframe_single_block_copy_true():
198+
# the copy=False/None cases are tested above in test_dataframe_values
199+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
200+
arr = np.array(df, copy=True)
201+
assert not np.shares_memory(arr, get_array(df, "a"))
202+
assert arr.flags.writeable is True
203+
177204

178205
def test_values_is_ea(using_copy_on_write):
179206
df = DataFrame({"a": date_range("2012-01-01", periods=3)})

0 commit comments

Comments
 (0)