Skip to content

Commit b4187ff

Browse files
authored
TST: de-duplicate fixtures (#44699)
1 parent 6b84ee7 commit b4187ff

File tree

17 files changed

+76
-220
lines changed

17 files changed

+76
-220
lines changed

pandas/conftest.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,21 @@ def multiindex_year_month_day_dataframe_random_data():
440440

441441

442442
@pytest.fixture
443-
def multiindex_dataframe_random_data():
444-
"""DataFrame with 2 level MultiIndex with random data"""
445-
index = MultiIndex(
443+
def lexsorted_two_level_string_multiindex():
444+
"""
445+
2-level MultiIndex, lexsorted, with string names.
446+
"""
447+
return MultiIndex(
446448
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
447449
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
448450
names=["first", "second"],
449451
)
452+
453+
454+
@pytest.fixture
455+
def multiindex_dataframe_random_data(lexsorted_two_level_string_multiindex):
456+
"""DataFrame with 2 level MultiIndex with random data"""
457+
index = lexsorted_two_level_string_multiindex
450458
return DataFrame(
451459
np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp")
452460
)

pandas/tests/arithmetic/conftest.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@ def switch_numexpr_min_elements(request):
2222
expr._MIN_ELEMENTS = _MIN_ELEMENTS
2323

2424

25-
# ------------------------------------------------------------------
26-
# Helper Functions
27-
28-
29-
def id_func(x):
30-
if isinstance(x, tuple):
31-
assert len(x) == 2
32-
return x[0].__name__ + "-" + str(x[1])
33-
else:
34-
return x.__name__
35-
36-
3725
# ------------------------------------------------------------------
3826

3927

@@ -228,7 +216,9 @@ def mismatched_freq(request):
228216
# ------------------------------------------------------------------
229217

230218

231-
@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, pd.array], ids=id_func)
219+
@pytest.fixture(
220+
params=[pd.Index, pd.Series, pd.DataFrame, pd.array], ids=lambda x: x.__name__
221+
)
232222
def box_with_array(request):
233223
"""
234224
Fixture to test behavior for Index, Series, DataFrame, and pandas Array
@@ -237,7 +227,9 @@ def box_with_array(request):
237227
return request.param
238228

239229

240-
@pytest.fixture(params=[pd.Index, pd.Series, tm.to_array, np.array, list], ids=id_func)
230+
@pytest.fixture(
231+
params=[pd.Index, pd.Series, tm.to_array, np.array, list], ids=lambda x: x.__name__
232+
)
241233
def box_1d_array(request):
242234
"""
243235
Fixture to test behavior for Index, Series, tm.to_array, numpy Array and list

pandas/tests/frame/methods/test_drop.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,8 @@ def test_drop_nonunique(self):
381381

382382
tm.assert_frame_equal(result, expected)
383383

384-
def test_drop_level(self):
385-
index = MultiIndex(
386-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
387-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
388-
names=["first", "second"],
389-
)
390-
frame = DataFrame(
391-
np.random.randn(10, 3),
392-
index=index,
393-
columns=Index(["A", "B", "C"], name="exp"),
394-
)
384+
def test_drop_level(self, multiindex_dataframe_random_data):
385+
frame = multiindex_dataframe_random_data
395386

396387
result = frame.drop(["bar", "qux"], level="first")
397388
expected = frame.iloc[[0, 1, 2, 5, 6]]

pandas/tests/frame/methods/test_sort_index.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
CategoricalDtype,
77
CategoricalIndex,
88
DataFrame,
9-
Index,
109
IntervalIndex,
1110
MultiIndex,
1211
RangeIndex,
@@ -591,17 +590,8 @@ def test_sort_index_and_reconstruction(self):
591590
assert result.columns.is_monotonic
592591

593592
# TODO: better name, de-duplicate with test_sort_index_level above
594-
def test_sort_index_level2(self):
595-
mi = MultiIndex(
596-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
597-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
598-
names=["first", "second"],
599-
)
600-
frame = DataFrame(
601-
np.random.randn(10, 3),
602-
index=mi,
603-
columns=Index(["A", "B", "C"], name="exp"),
604-
)
593+
def test_sort_index_level2(self, multiindex_dataframe_random_data):
594+
frame = multiindex_dataframe_random_data
605595

606596
df = frame.copy()
607597
df.index = np.arange(len(df))
@@ -639,34 +629,16 @@ def test_sort_index_level_large_cardinality(self):
639629
assert (result.dtypes.values == df.dtypes.values).all()
640630
assert result.index._lexsort_depth == 3
641631

642-
def test_sort_index_level_by_name(self):
643-
mi = MultiIndex(
644-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
645-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
646-
names=["first", "second"],
647-
)
648-
frame = DataFrame(
649-
np.random.randn(10, 3),
650-
index=mi,
651-
columns=Index(["A", "B", "C"], name="exp"),
652-
)
632+
def test_sort_index_level_by_name(self, multiindex_dataframe_random_data):
633+
frame = multiindex_dataframe_random_data
653634

654635
frame.index.names = ["first", "second"]
655636
result = frame.sort_index(level="second")
656637
expected = frame.sort_index(level=1)
657638
tm.assert_frame_equal(result, expected)
658639

659-
def test_sort_index_level_mixed(self):
660-
mi = MultiIndex(
661-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
662-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
663-
names=["first", "second"],
664-
)
665-
frame = DataFrame(
666-
np.random.randn(10, 3),
667-
index=mi,
668-
columns=Index(["A", "B", "C"], name="exp"),
669-
)
640+
def test_sort_index_level_mixed(self, multiindex_dataframe_random_data):
641+
frame = multiindex_dataframe_random_data
670642

671643
sorted_before = frame.sort_index(level=1)
672644

pandas/tests/groupby/conftest.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import (
5-
DataFrame,
6-
MultiIndex,
7-
)
4+
from pandas import DataFrame
85
import pandas._testing as tm
96
from pandas.core.groupby.base import (
107
reduction_kernels,
@@ -23,13 +20,8 @@ def as_index(request):
2320

2421

2522
@pytest.fixture
26-
def mframe():
27-
index = MultiIndex(
28-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
29-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
30-
names=["first", "second"],
31-
)
32-
return DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"])
23+
def mframe(multiindex_dataframe_random_data):
24+
return multiindex_dataframe_random_data
3325

3426

3527
@pytest.fixture

pandas/tests/groupby/test_allowlist.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
from pandas import (
1212
DataFrame,
13-
Index,
14-
MultiIndex,
1513
Series,
1614
date_range,
1715
)
@@ -89,16 +87,6 @@ def s_allowlist_fixture(request):
8987
return request.param
9088

9189

92-
@pytest.fixture
93-
def mframe():
94-
index = MultiIndex(
95-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
96-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
97-
names=["first", "second"],
98-
)
99-
return DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"])
100-
101-
10290
@pytest.fixture
10391
def df():
10492
return DataFrame(
@@ -174,18 +162,11 @@ def test_groupby_frame_allowlist(df_letters, df_allowlist_fixture):
174162

175163

176164
@pytest.fixture
177-
def raw_frame():
178-
index = MultiIndex(
179-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
180-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
181-
names=["first", "second"],
182-
)
183-
raw_frame = DataFrame(
184-
np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp")
185-
)
186-
raw_frame.iloc[1, [1, 2]] = np.nan
187-
raw_frame.iloc[7, [0, 1]] = np.nan
188-
return raw_frame
165+
def raw_frame(multiindex_dataframe_random_data):
166+
df = multiindex_dataframe_random_data
167+
df.iloc[1, [1, 2]] = np.nan
168+
df.iloc[7, [0, 1]] = np.nan
169+
return df
189170

190171

191172
@pytest.mark.parametrize("op", AGG_FUNCTIONS)

pandas/tests/indexes/multi/test_monotonic.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
)
88

99

10+
def test_is_monotonic_increasing_lexsorted(lexsorted_two_level_string_multiindex):
11+
# string ordering
12+
mi = lexsorted_two_level_string_multiindex
13+
assert mi.is_monotonic is False
14+
assert Index(mi.values).is_monotonic is False
15+
assert mi._is_strictly_monotonic_increasing is False
16+
assert Index(mi.values)._is_strictly_monotonic_increasing is False
17+
18+
1019
def test_is_monotonic_increasing():
1120
i = MultiIndex.from_product([np.arange(10), np.arange(10)], names=["one", "two"])
1221
assert i.is_monotonic is True
@@ -36,17 +45,6 @@ def test_is_monotonic_increasing():
3645
assert Index(i.values).is_monotonic is False
3746
assert Index(i.values)._is_strictly_monotonic_increasing is False
3847

39-
# string ordering
40-
i = MultiIndex(
41-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
42-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
43-
names=["first", "second"],
44-
)
45-
assert i.is_monotonic is False
46-
assert Index(i.values).is_monotonic is False
47-
assert i._is_strictly_monotonic_increasing is False
48-
assert Index(i.values)._is_strictly_monotonic_increasing is False
49-
5048
i = MultiIndex(
5149
levels=[["bar", "baz", "foo", "qux"], ["mom", "next", "zenith"]],
5250
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],

pandas/tests/indexes/test_common.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ def test_to_frame(self, name, index_flat):
4949
df = idx.to_frame(index=False, name=idx_name)
5050
assert df.index is not idx
5151

52-
def test_droplevel(self, index):
52+
def test_droplevel(self, index_flat):
5353
# GH 21115
54-
if isinstance(index, MultiIndex):
55-
# Tested separately in test_multi.py
56-
return
54+
# MultiIndex is tested separately in test_multi.py
55+
index = index_flat
5756

5857
assert index.droplevel([]).equals(index)
5958

60-
for level in index.name, [index.name]:
59+
for level in [index.name, [index.name]]:
6160
if isinstance(index.name, tuple) and level is index.name:
6261
# GH 21121 : droplevel with tuple name
6362
continue
@@ -174,8 +173,6 @@ def test_copy_name(self, index_flat):
174173
def test_copy_name2(self, index_flat):
175174
# GH#35592
176175
index = index_flat
177-
if isinstance(index, MultiIndex):
178-
return
179176

180177
assert index.copy(name="mario").name == "mario"
181178

@@ -192,7 +189,7 @@ def test_unique_level(self, index_flat):
192189

193190
# GH 17896
194191
expected = index.drop_duplicates()
195-
for level in 0, index.name, None:
192+
for level in [0, index.name, None]:
196193
result = index.unique(level=level)
197194
tm.assert_index_equal(result, expected)
198195

pandas/tests/io/formats/test_series_info.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@ def test_info_categorical():
3737

3838

3939
@pytest.mark.parametrize("verbose", [True, False])
40-
def test_info_series(verbose):
41-
index = MultiIndex(
42-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
43-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
44-
names=["first", "second"],
45-
)
46-
s = Series(range(len(index)), index=index, name="sth")
40+
def test_info_series(lexsorted_two_level_string_multiindex, verbose):
41+
index = lexsorted_two_level_string_multiindex
42+
ser = Series(range(len(index)), index=index, name="sth")
4743
buf = StringIO()
48-
s.info(verbose=verbose, buf=buf)
44+
ser.info(verbose=verbose, buf=buf)
4945
result = buf.getvalue()
5046

5147
expected = textwrap.dedent(
@@ -66,7 +62,7 @@ def test_info_series(verbose):
6662
expected += textwrap.dedent(
6763
f"""\
6864
dtypes: int64(1)
69-
memory usage: {s.memory_usage()}.0+ bytes
65+
memory usage: {ser.memory_usage()}.0+ bytes
7066
"""
7167
)
7268
assert result == expected

pandas/tests/io/pytables/test_append.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pandas as pd
1313
from pandas import (
1414
DataFrame,
15-
MultiIndex,
1615
Series,
1716
_testing as tm,
1817
concat,
@@ -638,13 +637,9 @@ def check_col(key, name, size):
638637
tm.assert_frame_equal(result, expected)
639638

640639

641-
def test_append_hierarchical(setup_path):
642-
index = MultiIndex(
643-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
644-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
645-
names=["foo", "bar"],
646-
)
647-
df = DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"])
640+
def test_append_hierarchical(setup_path, multiindex_dataframe_random_data):
641+
df = multiindex_dataframe_random_data
642+
df.columns.name = None
648643

649644
with ensure_clean_store(setup_path) as store:
650645
store.append("mi", df)

pandas/tests/io/pytables/test_round_trip.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pandas import (
1616
DataFrame,
1717
Index,
18-
MultiIndex,
1918
Series,
2019
_testing as tm,
2120
bdate_range,
@@ -419,13 +418,8 @@ def test_can_serialize_dates(setup_path):
419418
_check_roundtrip(frame, tm.assert_frame_equal, path=setup_path)
420419

421420

422-
def test_store_hierarchical(setup_path):
423-
index = MultiIndex(
424-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
425-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
426-
names=["foo", "bar"],
427-
)
428-
frame = DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"])
421+
def test_store_hierarchical(setup_path, multiindex_dataframe_random_data):
422+
frame = multiindex_dataframe_random_data
429423

430424
_check_roundtrip(frame, tm.assert_frame_equal, path=setup_path)
431425
_check_roundtrip(frame.T, tm.assert_frame_equal, path=setup_path)

0 commit comments

Comments
 (0)