|
1 | 1 | import numpy as np |
2 | 2 | import pytest |
3 | 3 |
|
| 4 | +from pandas.compat.numpy import np_version_gt2 |
| 5 | + |
4 | 6 | from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike |
5 | 7 | from pandas.core.dtypes.common import is_extension_array_dtype |
6 | 8 | from pandas.core.dtypes.dtypes import ExtensionDtype |
@@ -71,6 +73,25 @@ def test_array_interface(self, data): |
71 | 73 | expected = construct_1d_object_array_from_listlike(list(data)) |
72 | 74 | tm.assert_numpy_array_equal(result, expected) |
73 | 75 |
|
| 76 | + def test_array_interface_copy(self, data): |
| 77 | + result_copy1 = np.array(data, copy=True) |
| 78 | + result_copy2 = np.array(data, copy=True) |
| 79 | + assert not np.may_share_memory(result_copy1, result_copy2) |
| 80 | + |
| 81 | + if not np_version_gt2: |
| 82 | + # copy=False semantics are only supported in NumPy>=2. |
| 83 | + return |
| 84 | + |
| 85 | + try: |
| 86 | + result_nocopy1 = np.array(data, copy=False) |
| 87 | + except ValueError: |
| 88 | + # An error is always acceptable for `copy=False` |
| 89 | + return |
| 90 | + |
| 91 | + result_nocopy2 = np.array(data, copy=False) |
| 92 | + # If copy=False was given and did not raise, these must share the same data |
| 93 | + assert np.may_share_memory(result_nocopy1, result_nocopy2) |
| 94 | + |
74 | 95 | def test_is_extension_array_dtype(self, data): |
75 | 96 | assert is_extension_array_dtype(data) |
76 | 97 | assert is_extension_array_dtype(data.dtype) |
|
0 commit comments