Skip to content

Commit f9fc8e8

Browse files
fix base extension test
1 parent 9786ee3 commit f9fc8e8

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

pandas/tests/extension/base/interface.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import numpy as np
24
import pytest
35

@@ -82,13 +84,27 @@ def test_array_interface_copy(self, data):
8284
# copy=False semantics are only supported in NumPy>=2.
8385
return
8486

87+
warning_raised = False
8588
msg = "Starting with NumPy 2.0, the behavior of the 'copy' keyword has changed"
86-
with tm.assert_produces_warning(FutureWarning, match=msg):
89+
with warnings.catch_warnings(record=True) as w:
90+
warnings.simplefilter("always")
8791
result_nocopy1 = np.array(data, copy=False)
88-
89-
result_nocopy2 = np.array(data, copy=False)
90-
# If copy=False was given and did not raise, these must share the same data
91-
assert np.may_share_memory(result_nocopy1, result_nocopy2)
92+
assert len(w) <= 1
93+
if len(w):
94+
warning_raised = True
95+
assert msg in str(w[0].message)
96+
97+
with warnings.catch_warnings(record=True) as w:
98+
warnings.simplefilter("always")
99+
result_nocopy2 = np.array(data, copy=False)
100+
assert len(w) <= 1
101+
if len(w):
102+
warning_raised = True
103+
assert msg in str(w[0].message)
104+
105+
if not warning_raised:
106+
# If copy=False was given and did not raise, these must share the same data
107+
assert np.may_share_memory(result_nocopy1, result_nocopy2)
92108

93109
def test_is_extension_array_dtype(self, data):
94110
assert is_extension_array_dtype(data)

pandas/tests/extension/decimal/test_decimal.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import numpy as np
77
import pytest
88

9+
from pandas.compat.numpy import np_version_gt2
10+
911
import pandas as pd
1012
import pandas._testing as tm
1113
from pandas.tests.extension import base
@@ -289,6 +291,24 @@ def test_series_repr(self, data):
289291
def test_unary_ufunc_dunder_equivalence(self, data, ufunc):
290292
super().test_unary_ufunc_dunder_equivalence(data, ufunc)
291293

294+
def test_array_interface_copy(self, data):
295+
result_copy1 = np.array(data, copy=True)
296+
result_copy2 = np.array(data, copy=True)
297+
assert not np.may_share_memory(result_copy1, result_copy2)
298+
if not np_version_gt2:
299+
# copy=False semantics are only supported in NumPy>=2.
300+
return
301+
302+
try:
303+
result_nocopy1 = np.array(data, copy=False)
304+
except ValueError:
305+
# An error is always acceptable for `copy=False`
306+
return
307+
308+
result_nocopy2 = np.array(data, copy=False)
309+
# If copy=False was given and did not raise, these must share the same data
310+
assert np.may_share_memory(result_nocopy1, result_nocopy2)
311+
292312

293313
def test_take_na_value_other_decimal():
294314
arr = DecimalArray([decimal.Decimal("1.0"), decimal.Decimal("2.0")])

0 commit comments

Comments
 (0)