Skip to content

Commit 0a473b6

Browse files
jschendelproost
authored andcommitted
TST: Use fixtures instead of setup_method for index tests (pandas-dev#28865)
1 parent 62abe70 commit 0a473b6

File tree

12 files changed

+959
-963
lines changed

12 files changed

+959
-963
lines changed

pandas/tests/indexes/common.py

+339-381
Large diffs are not rendered by default.

pandas/tests/indexes/conftest.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@
55
from pandas.core.indexes.api import Index, MultiIndex
66
import pandas.util.testing as tm
77

8-
indices_list = [
9-
tm.makeUnicodeIndex(100),
10-
tm.makeStringIndex(100),
11-
tm.makeDateIndex(100),
12-
tm.makePeriodIndex(100),
13-
tm.makeTimedeltaIndex(100),
14-
tm.makeIntIndex(100),
15-
tm.makeUIntIndex(100),
16-
tm.makeRangeIndex(100),
17-
tm.makeFloatIndex(100),
18-
Index([True, False]),
19-
tm.makeCategoricalIndex(100),
20-
tm.makeIntervalIndex(100),
21-
Index([]),
22-
MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
23-
Index([0, 0, 1, 1, 2, 2]),
24-
]
25-
26-
27-
@pytest.fixture(params=indices_list, ids=lambda x: type(x).__name__)
8+
indices_dict = {
9+
"unicode": tm.makeUnicodeIndex(100),
10+
"string": tm.makeStringIndex(100),
11+
"datetime": tm.makeDateIndex(100),
12+
"period": tm.makePeriodIndex(100),
13+
"timedelta": tm.makeTimedeltaIndex(100),
14+
"int": tm.makeIntIndex(100),
15+
"uint": tm.makeUIntIndex(100),
16+
"range": tm.makeRangeIndex(100),
17+
"float": tm.makeFloatIndex(100),
18+
"bool": Index([True, False]),
19+
"categorical": tm.makeCategoricalIndex(100),
20+
"interval": tm.makeIntervalIndex(100),
21+
"empty": Index([]),
22+
"tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
23+
"repeats": Index([0, 0, 1, 1, 2, 2]),
24+
}
25+
26+
27+
@pytest.fixture(params=indices_dict.keys())
2828
def indices(request):
29-
return request.param
29+
# copy to avoid mutation, e.g. setting .name
30+
return indices_dict[request.param].copy()
3031

3132

3233
@pytest.fixture(params=[1, np.array(1, dtype=np.int64)])

pandas/tests/indexes/datetimelike.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ def test_view(self):
5858
tm.assert_index_equal(result, i_view)
5959

6060
def test_map_callable(self):
61-
expected = self.index + self.index.freq
62-
result = self.index.map(lambda x: x + x.freq)
61+
index = self.create_index()
62+
expected = index + index.freq
63+
result = index.map(lambda x: x + x.freq)
6364
tm.assert_index_equal(result, expected)
6465

6566
# map to NaT
66-
result = self.index.map(lambda x: pd.NaT if x == self.index[0] else x)
67-
expected = pd.Index([pd.NaT] + self.index[1:].tolist())
67+
result = index.map(lambda x: pd.NaT if x == index[0] else x)
68+
expected = pd.Index([pd.NaT] + index[1:].tolist())
6869
tm.assert_index_equal(result, expected)
6970

7071
@pytest.mark.parametrize(
@@ -75,23 +76,24 @@ def test_map_callable(self):
7576
],
7677
)
7778
def test_map_dictlike(self, mapper):
78-
expected = self.index + self.index.freq
79+
index = self.create_index()
80+
expected = index + index.freq
7981

8082
# don't compare the freqs
8183
if isinstance(expected, pd.DatetimeIndex):
8284
expected.freq = None
8385

84-
result = self.index.map(mapper(expected, self.index))
86+
result = index.map(mapper(expected, index))
8587
tm.assert_index_equal(result, expected)
8688

87-
expected = pd.Index([pd.NaT] + self.index[1:].tolist())
88-
result = self.index.map(mapper(expected, self.index))
89+
expected = pd.Index([pd.NaT] + index[1:].tolist())
90+
result = index.map(mapper(expected, index))
8991
tm.assert_index_equal(result, expected)
9092

9193
# empty map; these map to np.nan because we cannot know
9294
# to re-infer things
93-
expected = pd.Index([np.nan] * len(self.index))
94-
result = self.index.map(mapper([], []))
95+
expected = pd.Index([np.nan] * len(index))
96+
result = index.map(mapper([], []))
9597
tm.assert_index_equal(result, expected)
9698

9799
def test_asobject_deprecated(self):

pandas/tests/indexes/datetimes/test_datetimelike.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" generic tests from the Datetimelike class """
2+
import pytest
23

34
from pandas import DatetimeIndex, date_range
45
from pandas.util import testing as tm
@@ -9,12 +10,12 @@
910
class TestDatetimeIndex(DatetimeLike):
1011
_holder = DatetimeIndex
1112

12-
def setup_method(self, method):
13-
self.indices = dict(
14-
index=tm.makeDateIndex(10),
15-
index_dec=date_range("20130110", periods=10, freq="-1D"),
16-
)
17-
self.setup_indices()
13+
@pytest.fixture(
14+
params=[tm.makeDateIndex(10), date_range("20130110", periods=10, freq="-1D")],
15+
ids=["index_inc", "index_dec"],
16+
)
17+
def indices(self, request):
18+
return request.param
1819

1920
def create_index(self):
2021
return date_range("20130101", periods=5)

pandas/tests/indexes/interval/test_base.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class TestBase(Base):
1414

1515
_holder = IntervalIndex
1616

17-
def setup_method(self, method):
18-
self.index = IntervalIndex.from_arrays([0, 1], [1, 2])
19-
self.index_with_nan = IntervalIndex.from_tuples([(0, 1), np.nan, (1, 2)])
20-
self.indices = dict(intervalIndex=tm.makeIntervalIndex(10))
17+
@pytest.fixture
18+
def indices(self):
19+
return tm.makeIntervalIndex(10)
2120

2221
def create_index(self, closed="right"):
2322
return IntervalIndex.from_breaks(range(11), closed=closed)

pandas/tests/indexes/period/test_period.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
class TestPeriodIndex(DatetimeLike):
2626
_holder = PeriodIndex
2727

28-
def setup_method(self, method):
29-
self.indices = dict(
30-
index=tm.makePeriodIndex(10),
31-
index_dec=period_range("20130101", periods=10, freq="D")[::-1],
32-
)
33-
self.setup_indices()
28+
@pytest.fixture(
29+
params=[
30+
tm.makePeriodIndex(10),
31+
period_range("20130101", periods=10, freq="D")[::-1],
32+
],
33+
ids=["index_inc", "index_dec"],
34+
)
35+
def indices(self, request):
36+
return request.param
3437

3538
def create_index(self):
3639
return period_range("20130101", periods=5, freq="D")

0 commit comments

Comments
 (0)