Skip to content

Commit beb4859

Browse files
authored
REF: organize MultiIndex indexing tests (#31715)
1 parent 8aa2520 commit beb4859

File tree

6 files changed

+90
-87
lines changed

6 files changed

+90
-87
lines changed

pandas/tests/indexes/common.py

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ def test_create_index_existing_name(self):
167167
def test_numeric_compat(self):
168168

169169
idx = self.create_index()
170+
# Check that this doesn't cover MultiIndex case, if/when it does,
171+
# we can remove multi.test_compat.test_numeric_compat
172+
assert not isinstance(idx, MultiIndex)
173+
170174
with pytest.raises(TypeError, match="cannot perform __mul__"):
171175
idx * 1
172176
with pytest.raises(TypeError, match="cannot perform __rmul__"):

pandas/tests/indexes/multi/conftest.py

-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ def index_names():
4949
return ["first", "second"]
5050

5151

52-
@pytest.fixture
53-
def holder():
54-
# the MultiIndex constructor used to base compatibility with pickle
55-
return MultiIndex
56-
57-
5852
@pytest.fixture
5953
def compat_props():
6054
# a MultiIndex must have these properties associated with it

pandas/tests/indexes/multi/test_analytics.py

-77
Original file line numberDiff line numberDiff line change
@@ -146,83 +146,6 @@ def test_append_mixed_dtypes():
146146
tm.assert_index_equal(res, exp)
147147

148148

149-
def test_take(idx):
150-
indexer = [4, 3, 0, 2]
151-
result = idx.take(indexer)
152-
expected = idx[indexer]
153-
assert result.equals(expected)
154-
155-
# TODO: Remove Commented Code
156-
# if not isinstance(idx,
157-
# (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
158-
# GH 10791
159-
msg = "'MultiIndex' object has no attribute 'freq'"
160-
with pytest.raises(AttributeError, match=msg):
161-
idx.freq
162-
163-
164-
def test_take_invalid_kwargs(idx):
165-
idx = idx
166-
indices = [1, 2]
167-
168-
msg = r"take\(\) got an unexpected keyword argument 'foo'"
169-
with pytest.raises(TypeError, match=msg):
170-
idx.take(indices, foo=2)
171-
172-
msg = "the 'out' parameter is not supported"
173-
with pytest.raises(ValueError, match=msg):
174-
idx.take(indices, out=indices)
175-
176-
msg = "the 'mode' parameter is not supported"
177-
with pytest.raises(ValueError, match=msg):
178-
idx.take(indices, mode="clip")
179-
180-
181-
def test_take_fill_value():
182-
# GH 12631
183-
vals = [["A", "B"], [pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02")]]
184-
idx = pd.MultiIndex.from_product(vals, names=["str", "dt"])
185-
186-
result = idx.take(np.array([1, 0, -1]))
187-
exp_vals = [
188-
("A", pd.Timestamp("2011-01-02")),
189-
("A", pd.Timestamp("2011-01-01")),
190-
("B", pd.Timestamp("2011-01-02")),
191-
]
192-
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
193-
tm.assert_index_equal(result, expected)
194-
195-
# fill_value
196-
result = idx.take(np.array([1, 0, -1]), fill_value=True)
197-
exp_vals = [
198-
("A", pd.Timestamp("2011-01-02")),
199-
("A", pd.Timestamp("2011-01-01")),
200-
(np.nan, pd.NaT),
201-
]
202-
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
203-
tm.assert_index_equal(result, expected)
204-
205-
# allow_fill=False
206-
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
207-
exp_vals = [
208-
("A", pd.Timestamp("2011-01-02")),
209-
("A", pd.Timestamp("2011-01-01")),
210-
("B", pd.Timestamp("2011-01-02")),
211-
]
212-
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
213-
tm.assert_index_equal(result, expected)
214-
215-
msg = "When allow_fill=True and fill_value is not None, all indices must be >= -1"
216-
with pytest.raises(ValueError, match=msg):
217-
idx.take(np.array([1, 0, -2]), fill_value=True)
218-
with pytest.raises(ValueError, match=msg):
219-
idx.take(np.array([1, 0, -5]), fill_value=True)
220-
221-
msg = "index -5 is out of bounds for( axis 0 with)? size 4"
222-
with pytest.raises(IndexError, match=msg):
223-
idx.take(np.array([1, -5]))
224-
225-
226149
def test_iter(idx):
227150
result = list(idx)
228151
expected = [

pandas/tests/indexes/multi/test_compat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def test_ndarray_compat_properties(idx, compat_props):
112112
idx.values.nbytes
113113

114114

115-
def test_pickle_compat_construction(holder):
115+
def test_pickle_compat_construction():
116116
# this is testing for pickle compat
117117
# need an object to create with
118118
with pytest.raises(TypeError, match="Must pass both levels and codes"):
119-
holder()
119+
MultiIndex()

pandas/tests/indexes/multi/test_sorting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import random
2+
13
import numpy as np
24
import pytest
35

@@ -9,8 +11,6 @@
911

1012

1113
def test_sortlevel(idx):
12-
import random
13-
1414
tuples = list(idx)
1515
random.shuffle(tuples)
1616

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas as pd
5+
import pandas._testing as tm
6+
7+
8+
def test_take(idx):
9+
indexer = [4, 3, 0, 2]
10+
result = idx.take(indexer)
11+
expected = idx[indexer]
12+
assert result.equals(expected)
13+
14+
# FIXME: Remove Commented Code
15+
# if not isinstance(idx,
16+
# (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
17+
# GH 10791
18+
msg = "'MultiIndex' object has no attribute 'freq'"
19+
with pytest.raises(AttributeError, match=msg):
20+
idx.freq
21+
22+
23+
def test_take_invalid_kwargs(idx):
24+
idx = idx
25+
indices = [1, 2]
26+
27+
msg = r"take\(\) got an unexpected keyword argument 'foo'"
28+
with pytest.raises(TypeError, match=msg):
29+
idx.take(indices, foo=2)
30+
31+
msg = "the 'out' parameter is not supported"
32+
with pytest.raises(ValueError, match=msg):
33+
idx.take(indices, out=indices)
34+
35+
msg = "the 'mode' parameter is not supported"
36+
with pytest.raises(ValueError, match=msg):
37+
idx.take(indices, mode="clip")
38+
39+
40+
def test_take_fill_value():
41+
# GH 12631
42+
vals = [["A", "B"], [pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02")]]
43+
idx = pd.MultiIndex.from_product(vals, names=["str", "dt"])
44+
45+
result = idx.take(np.array([1, 0, -1]))
46+
exp_vals = [
47+
("A", pd.Timestamp("2011-01-02")),
48+
("A", pd.Timestamp("2011-01-01")),
49+
("B", pd.Timestamp("2011-01-02")),
50+
]
51+
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
52+
tm.assert_index_equal(result, expected)
53+
54+
# fill_value
55+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
56+
exp_vals = [
57+
("A", pd.Timestamp("2011-01-02")),
58+
("A", pd.Timestamp("2011-01-01")),
59+
(np.nan, pd.NaT),
60+
]
61+
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
62+
tm.assert_index_equal(result, expected)
63+
64+
# allow_fill=False
65+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
66+
exp_vals = [
67+
("A", pd.Timestamp("2011-01-02")),
68+
("A", pd.Timestamp("2011-01-01")),
69+
("B", pd.Timestamp("2011-01-02")),
70+
]
71+
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
72+
tm.assert_index_equal(result, expected)
73+
74+
msg = "When allow_fill=True and fill_value is not None, all indices must be >= -1"
75+
with pytest.raises(ValueError, match=msg):
76+
idx.take(np.array([1, 0, -2]), fill_value=True)
77+
with pytest.raises(ValueError, match=msg):
78+
idx.take(np.array([1, 0, -5]), fill_value=True)
79+
80+
msg = "index -5 is out of bounds for( axis 0 with)? size 4"
81+
with pytest.raises(IndexError, match=msg):
82+
idx.take(np.array([1, -5]))

0 commit comments

Comments
 (0)