Skip to content

Commit 504a484

Browse files
committed
modify tests relating to __array__ and assert FutureWarning
1 parent ac0573c commit 504a484

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

pandas/tests/arrays/sparse/test_array.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,12 @@ def test_array_interface(arr_data, arr):
500500
# copy=False semantics are only supported in NumPy>=2.
501501
return
502502

503-
# for sparse arrays, copy=False is never allowed
504-
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
503+
msg = "Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
504+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
505+
"is not possible, Pandas will follow this behavior starting with "
506+
"version 3.0. This conversion to NumPy requires a copy, but "
507+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead."
508+
with tm.assert_produces_warning(FutureWarning, match=msg):
505509
np.array(arr, copy=False)
506510

507511
# except when there are actually no sparse filled values

pandas/tests/base/test_conversion.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,12 @@ def test_to_numpy(arr, expected, zero_copy, index_or_series_or_array):
380380
return
381381

382382
if not zero_copy:
383-
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
384-
# An error is always acceptable for `copy=False`
383+
msg = "Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
384+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
385+
"is not possible, Pandas will follow this behavior starting with "
386+
"version 3.0. This conversion to NumPy requires a copy, but "
387+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead."
388+
with tm.assert_produces_warning(FutureWarning, match=msg):
385389
np.array(thing, copy=False)
386390

387391
else:

pandas/tests/extension/base/interface.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ def test_array_interface_copy(self, data):
8282
# copy=False semantics are only supported in NumPy>=2.
8383
return
8484

85-
try:
85+
msg = "Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
86+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
87+
"is not possible, Pandas will follow this behavior starting with "
88+
"version 3.0. This conversion to NumPy requires a copy, but "
89+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead."
90+
with tm.assert_produces_warning(FutureWarning, match=msg):
8691
result_nocopy1 = np.array(data, copy=False)
87-
except ValueError:
88-
# An error is always acceptable for `copy=False`
89-
return
9092

9193
result_nocopy2 = np.array(data, copy=False)
9294
# If copy=False was given and did not raise, these must share the same data

pandas/tests/indexes/multi/test_conversion.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ def test_array_interface(idx):
4747
return
4848

4949
# for MultiIndex, copy=False is never allowed
50-
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
50+
msg = "Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
51+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
52+
"is not possible, Pandas will follow this behavior starting with "
53+
"version 3.0. This conversion to NumPy requires a copy, but "
54+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead."
55+
with tm.assert_produces_warning(FutureWarning, match=msg):
5156
np.array(idx, copy=False)
5257

5358

0 commit comments

Comments
 (0)