Skip to content

Commit e4f81bb

Browse files
committed
Use less idx fixture in multi
1 parent 9f9a643 commit e4f81bb

18 files changed

+133
-83
lines changed

pandas/tests/indexes/multi/test_analytics.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import pandas._testing as tm
1212

1313

14-
def test_infer_objects(idx):
14+
def test_infer_objects():
15+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
1516
with pytest.raises(NotImplementedError, match="to_frame"):
1617
idx.infer_objects()
1718

1819

19-
def test_shift(idx):
20+
def test_shift():
2021
# GH8083 test the base class for shift
22+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
2123
msg = (
2224
"This method is only implemented for DatetimeIndex, PeriodIndex and "
2325
"TimedeltaIndex; Got type MultiIndex"
@@ -76,8 +78,9 @@ def test_truncate_multiindex():
7678
# TODO: reshape
7779

7880

79-
def test_reorder_levels(idx):
81+
def test_reorder_levels():
8082
# this blows up
83+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
8184
with pytest.raises(IndexError, match="^Too many levels"):
8285
idx.reorder_levels([2, 1, 0])
8386

@@ -174,9 +177,9 @@ def test_sub(idx):
174177
first.tolist() - idx[-3:]
175178

176179

177-
def test_map(idx):
180+
def test_map():
178181
# callable
179-
index = idx
182+
index = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
180183

181184
result = index.map(lambda x: x)
182185
tm.assert_index_equal(result, index)
@@ -235,10 +238,11 @@ def test_map_dictlike(idx, mapper):
235238
],
236239
ids=lambda func: func.__name__,
237240
)
238-
def test_numpy_ufuncs(idx, func):
241+
def test_numpy_ufuncs(func):
239242
# test ufuncs of numpy. see:
240243
# https://numpy.org/doc/stable/reference/ufuncs.html
241244

245+
idx = MultiIndex(levels=[["A", "B"]], codes=[[0, 1]])
242246
expected_exception = TypeError
243247
msg = (
244248
"loop of ufunc does not support argument 0 of type tuple which "
@@ -254,6 +258,7 @@ def test_numpy_ufuncs(idx, func):
254258
ids=lambda func: func.__name__,
255259
)
256260
def test_numpy_type_funcs(idx, func):
261+
idx = MultiIndex(levels=[["A", "B"]], codes=[[0, 1]])
257262
msg = (
258263
f"ufunc '{func.__name__}' not supported for the input types, and the inputs "
259264
"could not be safely coerced to any supported types according to "

pandas/tests/indexes/multi/test_astype.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
from pandas.core.dtypes.dtypes import CategoricalDtype
55

6+
from pandas import MultiIndex
67
import pandas._testing as tm
78

89

9-
def test_astype(idx):
10+
def test_astype():
11+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]], names=["foo"])
1012
expected = idx.copy()
1113
actual = idx.astype("O")
1214
tm.assert_copy(actual.levels, expected.levels)
@@ -18,7 +20,8 @@ def test_astype(idx):
1820

1921

2022
@pytest.mark.parametrize("ordered", [True, False])
21-
def test_astype_category(idx, ordered):
23+
def test_astype_category(ordered):
24+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
2225
# GH 18630
2326
msg = "> 1 ndim Categorical are not supported at this time"
2427
with pytest.raises(NotImplementedError, match=msg):

pandas/tests/indexes/multi/test_compat.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import pandas._testing as tm
77

88

9-
def test_numeric_compat(idx):
9+
def test_numeric_compat():
10+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
1011
with pytest.raises(TypeError, match="cannot perform __mul__"):
1112
idx * 1
1213

@@ -29,7 +30,8 @@ def test_numeric_compat(idx):
2930

3031

3132
@pytest.mark.parametrize("method", ["all", "any", "__invert__"])
32-
def test_logical_compat(idx, method):
33+
def test_logical_compat(method):
34+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
3335
msg = f"cannot perform {method}"
3436

3537
with pytest.raises(TypeError, match=msg):

pandas/tests/indexes/multi/test_constructors.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def test_from_tuples_empty():
385385
tm.assert_index_equal(result, expected)
386386

387387

388-
def test_from_tuples_index_values(idx):
388+
def test_from_tuples_index_values():
389+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
389390
result = MultiIndex.from_tuples(idx)
390391
assert (result.values == idx.values).all()
391392

pandas/tests/indexes/multi/test_conversion.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import pandas._testing as tm
1010

1111

12-
def test_to_numpy(idx):
12+
def test_to_numpy():
13+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
1314
result = idx.to_numpy()
1415
exp = idx.values
1516
tm.assert_numpy_array_equal(result, exp)

pandas/tests/indexes/multi/test_copy.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ def assert_multiindex_copied(copy, original):
2626
assert copy.sortorder == original.sortorder
2727

2828

29-
def test_copy(idx):
29+
def test_copy():
30+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
3031
i_copy = idx.copy()
3132

3233
assert_multiindex_copied(i_copy, idx)
3334

3435

35-
def test_shallow_copy(idx):
36+
def test_shallow_copy():
37+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
3638
i_copy = idx._view()
3739

3840
assert_multiindex_copied(i_copy, idx)
3941

4042

41-
def test_view(idx):
43+
def test_view():
44+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
4245
i_view = idx.view()
4346
assert_multiindex_copied(i_view, idx)
4447

pandas/tests/indexes/multi/test_duplicates.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,19 @@ def test_duplicate_meta_data():
145145
assert idx.drop_duplicates().names == idx.names
146146

147147

148-
def test_has_duplicates(idx, idx_dup):
149-
# see fixtures
148+
def test_has_duplicates():
149+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
150150
assert idx.is_unique is True
151151
assert idx.has_duplicates is False
152+
153+
154+
def test_has_duplicates_with_dups(idx_dup):
155+
# see fixtures
152156
assert idx_dup.is_unique is False
153157
assert idx_dup.has_duplicates is True
154158

159+
160+
def test_has_duplicates_other():
155161
mi = MultiIndex(
156162
levels=[[0, 1], [0, 1, 2]], codes=[[0, 0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 0, 1, 2]]
157163
)

pandas/tests/indexes/multi/test_equivalence.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import pandas._testing as tm
1313

1414

15-
def test_equals(idx):
15+
def test_equals():
16+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
1617
assert idx.equals(idx)
1718
assert idx.equals(idx.copy())
1819
assert idx.equals(idx.astype(object))
@@ -26,10 +27,6 @@ def test_equals(idx):
2627
assert idx.equals(same_values)
2728
assert same_values.equals(idx)
2829

29-
if idx.nlevels == 1:
30-
# do not test MultiIndex
31-
assert not idx.equals(Series(idx))
32-
3330

3431
def test_equals_op(idx):
3532
# GH9947, GH10637
@@ -132,7 +129,8 @@ def test_compare_tuple_strs():
132129
tm.assert_numpy_array_equal(result, expected)
133130

134131

135-
def test_equals_multi(idx):
132+
def test_equals_multi():
133+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
136134
assert idx.equals(idx)
137135
assert not idx.equals(idx.values)
138136
assert idx.equals(Index(idx.values))
@@ -141,6 +139,8 @@ def test_equals_multi(idx):
141139
assert not idx.equals(idx[:-1])
142140
assert not idx.equals(idx[-1])
143141

142+
143+
def test_equals_multi_different_levels(idx):
144144
# different number of levels
145145
index = MultiIndex(
146146
levels=[Index(list(range(4))), Index(list(range(4))), Index(list(range(4)))],
@@ -181,7 +181,8 @@ def test_equals_multi(idx):
181181
assert not idx.equals(index)
182182

183183

184-
def test_identical(idx):
184+
def test_identical():
185+
idx = MultiIndex(levels=[[0, 1], [2, 3]], codes=[[0, 1], [0, 1]])
185186
mi = idx.copy()
186187
mi2 = idx.copy()
187188
assert mi.identical(mi2)
@@ -249,12 +250,14 @@ def test_is_():
249250
assert not mi5.is_(mi)
250251

251252

252-
def test_is_all_dates(idx):
253+
def test_is_all_dates():
254+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
253255
assert not idx._is_all_dates
254256

255257

256-
def test_is_numeric(idx):
258+
def test_is_numeric():
257259
# MultiIndex is never numeric
260+
idx = MultiIndex(levels=[["A", "B"]], codes=[[0, 1]])
258261
assert not is_any_real_numeric_dtype(idx)
259262

260263

pandas/tests/indexes/multi/test_formats.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import pandas._testing as tm
1010

1111

12-
def test_format(idx):
12+
def test_format():
1313
msg = "MultiIndex.format is deprecated"
14+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
1415
with tm.assert_produces_warning(FutureWarning, match=msg):
1516
idx.format()
1617
idx[:0].format()
@@ -70,8 +71,9 @@ def test_unicode_string_with_unicode():
7071
str(idx)
7172

7273

73-
def test_repr_max_seq_item_setting(idx):
74+
def test_repr_max_seq_item_setting():
7475
# GH10182
76+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
7577
idx = idx.repeat(50)
7678
with pd.option_context("display.max_seq_items", None):
7779
repr(idx)

pandas/tests/indexes/multi/test_get_set.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ def test_set_codes(idx):
242242
assert result.equals(expected)
243243

244244

245-
def test_set_levels_codes_names_bad_input(idx):
245+
def test_set_levels_codes_names_bad_input():
246+
idx = MultiIndex(levels=[["A", "B"], ["B", "C"]], codes=[[0, 1], [0, 1]])
246247
levels, codes = idx.levels, idx.codes
247248
names = idx.names
248249

pandas/tests/indexes/multi/test_integrity.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ def test_take_invalid_kwargs():
163163
idx.take(indices, mode="clip")
164164

165165

166-
def test_isna_behavior(idx):
166+
def test_isna_behavior():
167167
# should not segfault GH5123
168168
# NOTE: if MI representation changes, may make sense to allow
169169
# isna(MI)
170+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
170171
msg = "isna is not defined for MultiIndex"
171172
with pytest.raises(NotImplementedError, match=msg):
172173
pd.isna(idx)
@@ -208,12 +209,14 @@ def test_mi_hashtable_populated_attribute_error(monkeypatch):
208209
df["a"].foo()
209210

210211

211-
def test_can_hold_identifiers(idx):
212+
def test_can_hold_identifiers():
213+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
212214
key = idx[0]
213215
assert idx._can_hold_identifiers_and_holds_name(key) is True
214216

215217

216-
def test_metadata_immutable(idx):
218+
def test_metadata_immutable():
219+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
217220
levels, codes = idx.levels, idx.codes
218221
# shouldn't be able to set at either the top level or base level
219222
mutable_regex = re.compile("does not support mutable operations")
@@ -265,7 +268,8 @@ def test_rangeindex_fallback_coercion_bug():
265268
tm.assert_index_equal(result, expected)
266269

267270

268-
def test_memory_usage(idx):
271+
def test_memory_usage():
272+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
269273
result = idx.memory_usage()
270274
if len(idx):
271275
idx.get_loc(idx[0])
@@ -285,5 +289,6 @@ def test_memory_usage(idx):
285289
assert result == 0
286290

287291

288-
def test_nlevels(idx):
289-
assert idx.nlevels == 2
292+
def test_nlevels():
293+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
294+
assert idx.nlevels == 1

pandas/tests/indexes/multi/test_join.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def test_join_level_corner_case(idx):
5050
idx.join(idx, level=1)
5151

5252

53-
def test_join_self(idx, join_type):
53+
def test_join_self(join_type):
54+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
5455
result = idx.join(idx, how=join_type)
5556
expected = idx
5657
if join_type == "outer":

pandas/tests/indexes/multi/test_missing.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import pandas._testing as tm
77

88

9-
def test_fillna(idx):
9+
def test_fillna():
1010
# GH 11343
11+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
1112
msg = "isna is not defined for MultiIndex"
1213
with pytest.raises(NotImplementedError, match=msg):
1314
idx.fillna(idx[0])
@@ -53,18 +54,19 @@ def test_dropna():
5354
tm.assert_index_equal(idx.dropna(how="all"), expected)
5455

5556

56-
def test_nulls(idx):
57+
def test_nulls():
5758
# this is really a smoke test for the methods
5859
# as these are adequately tested for function elsewhere
59-
60+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
6061
msg = "isna is not defined for MultiIndex"
6162
with pytest.raises(NotImplementedError, match=msg):
6263
idx.isna()
6364

6465

6566
@pytest.mark.xfail(reason="isna is not defined for MultiIndex")
66-
def test_hasnans_isnans(idx):
67+
def test_hasnans_isnans():
6768
# GH 11343, added tests for hasnans / isnans
69+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
6870
index = idx.copy()
6971

7072
# cases in indices doesn't include NaN

pandas/tests/indexes/multi/test_reindex.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import pandas._testing as tm
1010

1111

12-
def test_reindex(idx):
12+
def test_reindex():
13+
idx = MultiIndex(
14+
levels=[list(range(5)), list(range(1, 6))],
15+
codes=[list(range(5)), list(range(5))],
16+
names=["first", "second"],
17+
)
1318
result, indexer = idx.reindex(list(idx[:4]))
1419
assert isinstance(result, MultiIndex)
1520
assert result.names == ["first", "second"]
@@ -92,7 +97,8 @@ def test_reindex_lvl_preserves_type_if_target_is_empty_list_or_array(
9297
assert mi.reindex([], level=1)[0].levels[1].dtype == dti.dtype
9398

9499

95-
def test_reindex_base(idx):
100+
def test_reindex_base():
101+
idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]])
96102
expected = np.arange(idx.size, dtype=np.intp)
97103

98104
actual = idx.get_indexer(idx)

0 commit comments

Comments
 (0)