Skip to content

Commit 83f0d71

Browse files
authored
TST: Use fixtures instead of setup_method (#46713)
1 parent afbe1ed commit 83f0d71

File tree

13 files changed

+419
-422
lines changed

13 files changed

+419
-422
lines changed

pandas/tests/frame/test_query_eval.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -1094,20 +1094,18 @@ def test_query_string_scalar_variable(self, parser, engine):
10941094

10951095

10961096
class TestDataFrameEvalWithFrame:
1097-
def setup_method(self):
1098-
self.frame = DataFrame(np.random.randn(10, 3), columns=list("abc"))
1099-
1100-
def teardown_method(self):
1101-
del self.frame
1097+
@pytest.fixture
1098+
def frame(self):
1099+
return DataFrame(np.random.randn(10, 3), columns=list("abc"))
11021100

1103-
def test_simple_expr(self, parser, engine):
1104-
res = self.frame.eval("a + b", engine=engine, parser=parser)
1105-
expect = self.frame.a + self.frame.b
1101+
def test_simple_expr(self, frame, parser, engine):
1102+
res = frame.eval("a + b", engine=engine, parser=parser)
1103+
expect = frame.a + frame.b
11061104
tm.assert_series_equal(res, expect)
11071105

1108-
def test_bool_arith_expr(self, parser, engine):
1109-
res = self.frame.eval("a[a < 1] + b", engine=engine, parser=parser)
1110-
expect = self.frame.a[self.frame.a < 1] + self.frame.b
1106+
def test_bool_arith_expr(self, frame, parser, engine):
1107+
res = frame.eval("a[a < 1] + b", engine=engine, parser=parser)
1108+
expect = frame.a[frame.a < 1] + frame.b
11111109
tm.assert_series_equal(res, expect)
11121110

11131111
@pytest.mark.parametrize("op", ["+", "-", "*", "/"])

pandas/tests/indexes/datetimes/test_setops.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -417,27 +417,25 @@ def test_intersection_non_tick_no_fastpath(self):
417417

418418

419419
class TestBusinessDatetimeIndex:
420-
def setup_method(self):
421-
self.rng = bdate_range(START, END)
422-
423420
def test_union(self, sort):
421+
rng = bdate_range(START, END)
424422
# overlapping
425-
left = self.rng[:10]
426-
right = self.rng[5:10]
423+
left = rng[:10]
424+
right = rng[5:10]
427425

428426
the_union = left.union(right, sort=sort)
429427
assert isinstance(the_union, DatetimeIndex)
430428

431429
# non-overlapping, gap in middle
432-
left = self.rng[:5]
433-
right = self.rng[10:]
430+
left = rng[:5]
431+
right = rng[10:]
434432

435433
the_union = left.union(right, sort=sort)
436434
assert isinstance(the_union, Index)
437435

438436
# non-overlapping, no gap
439-
left = self.rng[:5]
440-
right = self.rng[5:10]
437+
left = rng[:5]
438+
right = rng[5:10]
441439

442440
the_union = left.union(right, sort=sort)
443441
assert isinstance(the_union, DatetimeIndex)
@@ -452,7 +450,7 @@ def test_union(self, sort):
452450
# overlapping, but different offset
453451
rng = date_range(START, END, freq=BMonthEnd())
454452

455-
the_union = self.rng.union(rng, sort=sort)
453+
the_union = rng.union(rng, sort=sort)
456454
assert isinstance(the_union, DatetimeIndex)
457455

458456
def test_union_not_cacheable(self, sort):
@@ -555,27 +553,25 @@ def test_intersection_duplicates(self, sort):
555553

556554

557555
class TestCustomDatetimeIndex:
558-
def setup_method(self):
559-
self.rng = bdate_range(START, END, freq="C")
560-
561556
def test_union(self, sort):
562557
# overlapping
563-
left = self.rng[:10]
564-
right = self.rng[5:10]
558+
rng = bdate_range(START, END, freq="C")
559+
left = rng[:10]
560+
right = rng[5:10]
565561

566562
the_union = left.union(right, sort=sort)
567563
assert isinstance(the_union, DatetimeIndex)
568564

569565
# non-overlapping, gap in middle
570-
left = self.rng[:5]
571-
right = self.rng[10:]
566+
left = rng[:5]
567+
right = rng[10:]
572568

573569
the_union = left.union(right, sort)
574570
assert isinstance(the_union, Index)
575571

576572
# non-overlapping, no gap
577-
left = self.rng[:5]
578-
right = self.rng[5:10]
573+
left = rng[:5]
574+
right = rng[5:10]
579575

580576
the_union = left.union(right, sort=sort)
581577
assert isinstance(the_union, DatetimeIndex)
@@ -587,7 +583,7 @@ def test_union(self, sort):
587583
# overlapping, but different offset
588584
rng = date_range(START, END, freq=BMonthEnd())
589585

590-
the_union = self.rng.union(rng, sort=sort)
586+
the_union = rng.union(rng, sort=sort)
591587
assert isinstance(the_union, DatetimeIndex)
592588

593589
def test_intersection_bug(self):

pandas/tests/indexing/test_categorical.py

+40-36
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,37 @@
2020
from pandas.api.types import CategoricalDtype as CDT
2121

2222

23-
class TestCategoricalIndex:
24-
def setup_method(self):
23+
@pytest.fixture
24+
def df():
25+
return DataFrame(
26+
{
27+
"A": np.arange(6, dtype="int64"),
28+
},
29+
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cab")), name="B"),
30+
)
31+
32+
33+
@pytest.fixture
34+
def df2():
35+
return DataFrame(
36+
{
37+
"A": np.arange(6, dtype="int64"),
38+
},
39+
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
40+
)
2541

26-
self.df = DataFrame(
27-
{
28-
"A": np.arange(6, dtype="int64"),
29-
},
30-
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cab")), name="B"),
31-
)
32-
self.df2 = DataFrame(
33-
{
34-
"A": np.arange(6, dtype="int64"),
35-
},
36-
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
37-
)
3842

39-
def test_loc_scalar(self):
43+
class TestCategoricalIndex:
44+
def test_loc_scalar(self, df):
4045
dtype = CDT(list("cab"))
41-
result = self.df.loc["a"]
46+
result = df.loc["a"]
4247
bidx = Series(list("aaa"), name="B").astype(dtype)
4348
assert bidx.dtype == dtype
4449

4550
expected = DataFrame({"A": [0, 1, 5]}, index=Index(bidx))
4651
tm.assert_frame_equal(result, expected)
4752

48-
df = self.df.copy()
53+
df = df.copy()
4954
df.loc["a"] = 20
5055
bidx2 = Series(list("aabbca"), name="B").astype(dtype)
5156
assert bidx2.dtype == dtype
@@ -68,9 +73,8 @@ def test_loc_scalar(self):
6873
df2.loc["d"] = 10
6974
tm.assert_frame_equal(df2, expected)
7075

71-
def test_loc_setitem_with_expansion_non_category(self):
76+
def test_loc_setitem_with_expansion_non_category(self, df):
7277
# Setting-with-expansion with a new key "d" that is not among caegories
73-
df = self.df
7478
df.loc["a"] = 20
7579

7680
# Setting a new row on an existing column
@@ -97,9 +101,9 @@ def test_loc_setitem_with_expansion_non_category(self):
97101
)
98102
tm.assert_frame_equal(df4, expected3)
99103

100-
def test_loc_getitem_scalar_non_category(self):
104+
def test_loc_getitem_scalar_non_category(self, df):
101105
with pytest.raises(KeyError, match="^1$"):
102-
self.df.loc[1]
106+
df.loc[1]
103107

104108
def test_slicing(self):
105109
cat = Series(Categorical([1, 2, 3, 4]))
@@ -287,31 +291,31 @@ def test_slicing_doc_examples(self):
287291
)
288292
tm.assert_frame_equal(result, expected)
289293

290-
def test_loc_getitem_listlike_labels(self):
294+
def test_loc_getitem_listlike_labels(self, df):
291295
# list of labels
292-
result = self.df.loc[["c", "a"]]
293-
expected = self.df.iloc[[4, 0, 1, 5]]
296+
result = df.loc[["c", "a"]]
297+
expected = df.iloc[[4, 0, 1, 5]]
294298
tm.assert_frame_equal(result, expected, check_index_type=True)
295299

296-
def test_loc_getitem_listlike_unused_category(self):
300+
def test_loc_getitem_listlike_unused_category(self, df2):
297301
# GH#37901 a label that is in index.categories but not in index
298302
# listlike containing an element in the categories but not in the values
299303
with pytest.raises(KeyError, match=re.escape("['e'] not in index")):
300-
self.df2.loc[["a", "b", "e"]]
304+
df2.loc[["a", "b", "e"]]
301305

302-
def test_loc_getitem_label_unused_category(self):
306+
def test_loc_getitem_label_unused_category(self, df2):
303307
# element in the categories but not in the values
304308
with pytest.raises(KeyError, match=r"^'e'$"):
305-
self.df2.loc["e"]
309+
df2.loc["e"]
306310

307-
def test_loc_getitem_non_category(self):
311+
def test_loc_getitem_non_category(self, df2):
308312
# not all labels in the categories
309313
with pytest.raises(KeyError, match=re.escape("['d'] not in index")):
310-
self.df2.loc[["a", "d"]]
314+
df2.loc[["a", "d"]]
311315

312-
def test_loc_setitem_expansion_label_unused_category(self):
316+
def test_loc_setitem_expansion_label_unused_category(self, df2):
313317
# assigning with a label that is in the categories but not in the index
314-
df = self.df2.copy()
318+
df = df2.copy()
315319
df.loc["e"] = 20
316320
result = df.loc[["a", "b", "e"]]
317321
exp_index = CategoricalIndex(list("aaabbe"), categories=list("cabe"), name="B")
@@ -450,17 +454,17 @@ def test_ix_categorical_index_non_unique(self):
450454
)
451455
tm.assert_frame_equal(cdf.loc[:, ["X", "Y"]], expect)
452456

453-
def test_loc_slice(self):
457+
def test_loc_slice(self, df):
454458
# GH9748
455459
msg = (
456460
"cannot do slice indexing on CategoricalIndex with these "
457461
r"indexers \[1\] of type int"
458462
)
459463
with pytest.raises(TypeError, match=msg):
460-
self.df.loc[1:5]
464+
df.loc[1:5]
461465

462-
result = self.df.loc["b":"c"]
463-
expected = self.df.iloc[[2, 3, 4]]
466+
result = df.loc["b":"c"]
467+
expected = df.iloc[[2, 3, 4]]
464468
tm.assert_frame_equal(result, expected)
465469

466470
def test_loc_and_at_with_categorical_index(self):

pandas/tests/internals/test_internals.py

+23-24
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,12 @@ def create_mgr(descr, item_shape=None):
243243
)
244244

245245

246-
class TestBlock:
247-
def setup_method(self):
248-
self.fblock = create_block("float", [0, 2, 4])
249-
self.cblock = create_block("complex", [7])
250-
self.oblock = create_block("object", [1, 3])
251-
self.bool_block = create_block("bool", [5])
246+
@pytest.fixture
247+
def fblock():
248+
return create_block("float", [0, 2, 4])
249+
252250

251+
class TestBlock:
253252
def test_constructor(self):
254253
int32block = create_block("i4", [0])
255254
assert int32block.dtype == np.int32
@@ -267,24 +266,24 @@ def test_pickle(self, typ, data):
267266
blk = create_block(typ, data)
268267
assert_block_equal(tm.round_trip_pickle(blk), blk)
269268

270-
def test_mgr_locs(self):
271-
assert isinstance(self.fblock.mgr_locs, BlockPlacement)
269+
def test_mgr_locs(self, fblock):
270+
assert isinstance(fblock.mgr_locs, BlockPlacement)
272271
tm.assert_numpy_array_equal(
273-
self.fblock.mgr_locs.as_array, np.array([0, 2, 4], dtype=np.intp)
272+
fblock.mgr_locs.as_array, np.array([0, 2, 4], dtype=np.intp)
274273
)
275274

276-
def test_attrs(self):
277-
assert self.fblock.shape == self.fblock.values.shape
278-
assert self.fblock.dtype == self.fblock.values.dtype
279-
assert len(self.fblock) == len(self.fblock.values)
275+
def test_attrs(self, fblock):
276+
assert fblock.shape == fblock.values.shape
277+
assert fblock.dtype == fblock.values.dtype
278+
assert len(fblock) == len(fblock.values)
280279

281-
def test_copy(self):
282-
cop = self.fblock.copy()
283-
assert cop is not self.fblock
284-
assert_block_equal(self.fblock, cop)
280+
def test_copy(self, fblock):
281+
cop = fblock.copy()
282+
assert cop is not fblock
283+
assert_block_equal(fblock, cop)
285284

286-
def test_delete(self):
287-
newb = self.fblock.copy()
285+
def test_delete(self, fblock):
286+
newb = fblock.copy()
288287
locs = newb.mgr_locs
289288
nb = newb.delete(0)
290289
assert newb.mgr_locs is locs
@@ -297,7 +296,7 @@ def test_delete(self):
297296
assert not (newb.values[0] == 1).all()
298297
assert (nb.values[0] == 1).all()
299298

300-
newb = self.fblock.copy()
299+
newb = fblock.copy()
301300
locs = newb.mgr_locs
302301
nb = newb.delete(1)
303302
assert newb.mgr_locs is locs
@@ -308,15 +307,15 @@ def test_delete(self):
308307
assert not (newb.values[1] == 2).all()
309308
assert (nb.values[1] == 2).all()
310309

311-
newb = self.fblock.copy()
310+
newb = fblock.copy()
312311
locs = newb.mgr_locs
313312
nb = newb.delete(2)
314313
tm.assert_numpy_array_equal(
315314
nb.mgr_locs.as_array, np.array([0, 2], dtype=np.intp)
316315
)
317316
assert (nb.values[1] == 1).all()
318317

319-
newb = self.fblock.copy()
318+
newb = fblock.copy()
320319

321320
with pytest.raises(IndexError, match=None):
322321
newb.delete(3)
@@ -357,9 +356,9 @@ def test_split(self):
357356
for res, exp in zip(result, expected):
358357
assert_block_equal(res, exp)
359358

360-
def test_is_categorical_deprecated(self):
359+
def test_is_categorical_deprecated(self, fblock):
361360
# GH#40571
362-
blk = self.fblock
361+
blk = fblock
363362
with tm.assert_produces_warning(DeprecationWarning):
364363
blk.is_categorical
365364

0 commit comments

Comments
 (0)