Skip to content

Commit 6e08cc8

Browse files
add specific tests
1 parent ab4b976 commit 6e08cc8

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

pandas/core/arrays/masked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def to_numpy(
539539
with warnings.catch_warnings():
540540
warnings.filterwarnings("ignore", category=RuntimeWarning)
541541
data = self._data.astype(dtype, copy=copy)
542-
if self._readonly and astype_is_view(self.dtype, dtype):
542+
if self._readonly and not copy and astype_is_view(self.dtype, dtype):
543543
data = data.view()
544544
data.flags.writeable = False
545545
return data

pandas/tests/arrays/boolean/test_construction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,13 @@ def test_to_numpy_copy():
323323
result = arr.to_numpy(dtype=bool, copy=True)
324324
result[0] = False
325325
tm.assert_extension_array_equal(arr, pd.array([True, False, True], dtype="boolean"))
326+
327+
328+
def test_to_numpy_readonly():
329+
arr = pd.array([True, False, True], dtype="boolean")
330+
arr._readonly = True
331+
result = arr.to_numpy(dtype=bool)
332+
assert not result.flags.writeable
333+
334+
result = arr.to_numpy(dtype="int64")
335+
assert result.flags.writeable

pandas/tests/arrays/floating/test_to_numpy.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,26 @@ def test_to_numpy_copy():
130130
result = arr.to_numpy(dtype="float64", copy=True)
131131
result[0] = 10
132132
tm.assert_extension_array_equal(arr, pd.array([0.1, 0.2, 0.3], dtype="Float64"))
133+
134+
135+
def test_to_numpy_readonly():
136+
arr = pd.array([0.1, 0.2, 0.3], dtype="Float64")
137+
arr._readonly = True
138+
result = arr.to_numpy(dtype="float64")
139+
assert not result.flags.writeable
140+
141+
result = arr.to_numpy(dtype="float64", copy=True)
142+
assert result.flags.writeable
143+
144+
result = arr.to_numpy(dtype="float32")
145+
assert result.flags.writeable
146+
147+
result = arr.to_numpy(dtype="object")
148+
assert result.flags.writeable
149+
150+
151+
def test_asarray_readonly():
152+
arr = pd.array([0.1, 0.2, 0.3], dtype="Float64")
153+
arr._readonly = True
154+
result = np.asarray(arr, copy=False)
155+
assert not result.flags.writeable

pandas/tests/arrays/integer/test_dtypes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ def test_to_numpy_na_raises(dtype):
276276
a.to_numpy(dtype=dtype)
277277

278278

279+
def test_to_numpy_readonly():
280+
arr = pd.array([0, 1], dtype="Int64")
281+
arr._readonly = True
282+
result = arr.to_numpy()
283+
assert not result.flags.writeable
284+
285+
result = arr.to_numpy(dtype="int64", copy=True)
286+
assert result.flags.writeable
287+
288+
result = arr.to_numpy(dtype="int32")
289+
assert result.flags.writeable
290+
291+
result = arr.to_numpy(dtype="object")
292+
assert result.flags.writeable
293+
294+
279295
def test_astype_str(using_infer_string):
280296
a = pd.array([1, 2, None], dtype="Int64")
281297

pandas/tests/arrays/numpy_/test_numpy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,33 @@ def test_to_numpy():
155155
tm.assert_numpy_array_equal(result, expected)
156156

157157

158+
def test_to_numpy_readonly():
159+
arr = NumpyExtensionArray(np.array([1, 2, 3]))
160+
arr._readonly = True
161+
result = arr.to_numpy()
162+
assert not result.flags.writeable
163+
164+
result = arr.to_numpy(copy=True)
165+
assert result.flags.writeable
166+
167+
result = arr.to_numpy(dtype="f8")
168+
assert result.flags.writeable
169+
170+
171+
@pytest.mark.parametrize("dtype", [None, "int64"])
172+
def test_asarray_readonly(dtype):
173+
arr = NumpyExtensionArray(np.array([1, 2, 3]))
174+
arr._readonly = True
175+
result = np.asarray(arr, dtype=dtype)
176+
assert not result.flags.writeable
177+
178+
result = np.asarray(arr, dtype=dtype, copy=True)
179+
assert result.flags.writeable
180+
181+
result = np.asarray(arr, dtype=dtype, copy=False)
182+
assert not result.flags.writeable
183+
184+
158185
# ----------------------------------------------------------------------------
159186
# Setitem
160187

pandas/tests/arrays/string_/test_string.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,16 @@ def test_to_numpy_na_value(dtype, nulls_fixture):
753753
tm.assert_numpy_array_equal(result, expected)
754754

755755

756+
def test_to_numpy_readonly(dtype):
757+
arr = pd.array(["a", pd.NA, "b"], dtype=dtype)
758+
arr._readonly = True
759+
result = arr.to_numpy()
760+
if dtype.storage == "python":
761+
assert not result.flags.writeable
762+
else:
763+
assert result.flags.writeable
764+
765+
756766
def test_isin(dtype, fixed_now_ts):
757767
s = pd.Series(["a", "b", None], dtype=dtype)
758768

pandas/tests/arrays/test_datetimelike.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,28 @@ def test_to_numpy_extra(arr):
12741274
tm.assert_equal(arr, original)
12751275

12761276

1277+
@pytest.mark.parametrize(
1278+
"arr",
1279+
[
1280+
pd.date_range("2000", periods=4)._values,
1281+
pd.timedelta_range("2000", periods=4)._values,
1282+
],
1283+
)
1284+
def test_to_numpy_extra_readonly(arr):
1285+
arr[0] = NaT
1286+
original = arr.copy()
1287+
arr._readonly = True
1288+
1289+
result = arr.to_numpy(dtype=object)
1290+
assert result.flags.writeable
1291+
1292+
# numpy does not do zero-copy conversion from M8 to i8
1293+
result = arr.to_numpy(dtype="int64")
1294+
assert result.flags.writeable
1295+
1296+
tm.assert_equal(arr, original)
1297+
1298+
12771299
@pytest.mark.parametrize("as_index", [True, False])
12781300
@pytest.mark.parametrize(
12791301
"values",

0 commit comments

Comments
 (0)