Skip to content

Commit 9927903

Browse files
add specific test for MultiIndex
1 parent 357f8a0 commit 9927903

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pandas/tests/indexes/multi/test_conversion.py

Lines changed: 36 additions & 0 deletions
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
import pandas as pd
57
from pandas import (
68
DataFrame,
@@ -16,6 +18,40 @@ def test_to_numpy(idx):
1618
tm.assert_numpy_array_equal(result, exp)
1719

1820

21+
def test_array_interface(idx):
22+
# https://github.com/pandas-dev/pandas/pull/60046
23+
result = np.asarray(idx)
24+
expected = np.empty((6,), dtype=object)
25+
expected[:] = [
26+
("foo", "one"),
27+
("foo", "two"),
28+
("bar", "one"),
29+
("baz", "two"),
30+
("qux", "one"),
31+
("qux", "two"),
32+
]
33+
tm.assert_numpy_array_equal(result, expected)
34+
35+
# it always gives a copy by default, but the values are cached, so results
36+
# are still sharing memory
37+
result_copy1 = np.asarray(idx)
38+
result_copy2 = np.asarray(idx)
39+
assert np.may_share_memory(result_copy1, result_copy2)
40+
41+
# with explicit copy=True, then it is an actual copy
42+
result_copy1 = np.array(idx, copy=True)
43+
result_copy2 = np.array(idx, copy=True)
44+
assert not np.may_share_memory(result_copy1, result_copy2)
45+
46+
if not np_version_gt2:
47+
# copy=False semantics are only supported in NumPy>=2.
48+
return
49+
50+
# for MultiIndex, copy=False is never allowed
51+
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
52+
np.array(idx, copy=False)
53+
54+
1955
def test_to_frame():
2056
tuples = [(1, "one"), (1, "two"), (2, "one"), (2, "two")]
2157

0 commit comments

Comments
 (0)