Skip to content

Commit 7fe140e

Browse files
TST: add extra test case for np.array(obj, copy=False) read-only behaviour (pandas-dev#60191)
1 parent 909c6da commit 7fe140e

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
@@ -2014,6 +2014,12 @@ def empty(self) -> bool:
20142014
def __array__(
20152015
self, dtype: npt.DTypeLike | None = None, copy: bool | None = None
20162016
) -> np.ndarray:
2017+
if copy is False and not self._mgr.is_single_block and not self.empty:
2018+
# check this manually, otherwise ._values will already return a copy
2019+
# and np.array(values, copy=False) will not raise an error
2020+
raise ValueError(
2021+
"Unable to avoid copy while creating an array as requested."
2022+
)
20172023
values = self._values
20182024
if copy is None:
20192025
# 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(method):
2228
ser = Series([1, 2, 3], name="name")
@@ -40,8 +46,12 @@ def test_series_values(method):
4046

4147
@pytest.mark.parametrize(
4248
"method",
43-
[lambda df: df.values, lambda df: np.asarray(df)],
44-
ids=["values", "asarray"],
49+
[
50+
lambda df: df.values,
51+
lambda df: np.asarray(df),
52+
lambda ser: np.array(ser, copy=False),
53+
],
54+
ids=["values", "asarray", "array"],
4555
)
4656
def test_dataframe_values(method):
4757
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
@@ -82,7 +92,7 @@ def test_series_to_numpy():
8292
ser.iloc[0] = 0
8393
assert ser.values[0] == 0
8494

85-
# specify copy=False gives a writeable array
95+
# specify copy=True gives a writeable array
8696
ser = Series([1, 2, 3], name="name")
8797
arr = ser.to_numpy(copy=True)
8898
assert not np.shares_memory(arr, get_array(ser, "name"))
@@ -130,6 +140,23 @@ def test_dataframe_multiple_numpy_dtypes():
130140
assert not np.shares_memory(arr, get_array(df, "a"))
131141
assert arr.flags.writeable is True
132142

143+
if np_version_gt2:
144+
# copy=False semantics are only supported in NumPy>=2.
145+
146+
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
147+
arr = np.array(df, copy=False)
148+
149+
arr = np.array(df, copy=True)
150+
assert arr.flags.writeable is True
151+
152+
153+
def test_dataframe_single_block_copy_true():
154+
# the copy=False/None cases are tested above in test_dataframe_values
155+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
156+
arr = np.array(df, copy=True)
157+
assert not np.shares_memory(arr, get_array(df, "a"))
158+
assert arr.flags.writeable is True
159+
133160

134161
def test_values_is_ea():
135162
df = DataFrame({"a": date_range("2012-01-01", periods=3)})

0 commit comments

Comments
 (0)