Skip to content

TST: Use fixtures instead of setup_method #46713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,20 +1094,18 @@ def test_query_string_scalar_variable(self, parser, engine):


class TestDataFrameEvalWithFrame:
def setup_method(self):
self.frame = DataFrame(np.random.randn(10, 3), columns=list("abc"))

def teardown_method(self):
del self.frame
@pytest.fixture
def frame(self):
return DataFrame(np.random.randn(10, 3), columns=list("abc"))

def test_simple_expr(self, parser, engine):
res = self.frame.eval("a + b", engine=engine, parser=parser)
expect = self.frame.a + self.frame.b
def test_simple_expr(self, frame, parser, engine):
res = frame.eval("a + b", engine=engine, parser=parser)
expect = frame.a + frame.b
tm.assert_series_equal(res, expect)

def test_bool_arith_expr(self, parser, engine):
res = self.frame.eval("a[a < 1] + b", engine=engine, parser=parser)
expect = self.frame.a[self.frame.a < 1] + self.frame.b
def test_bool_arith_expr(self, frame, parser, engine):
res = frame.eval("a[a < 1] + b", engine=engine, parser=parser)
expect = frame.a[frame.a < 1] + frame.b
tm.assert_series_equal(res, expect)

@pytest.mark.parametrize("op", ["+", "-", "*", "/"])
Expand Down
36 changes: 16 additions & 20 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,27 +417,25 @@ def test_intersection_non_tick_no_fastpath(self):


class TestBusinessDatetimeIndex:
def setup_method(self):
self.rng = bdate_range(START, END)

def test_union(self, sort):
rng = bdate_range(START, END)
# overlapping
left = self.rng[:10]
right = self.rng[5:10]
left = rng[:10]
right = rng[5:10]

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

# non-overlapping, gap in middle
left = self.rng[:5]
right = self.rng[10:]
left = rng[:5]
right = rng[10:]

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

# non-overlapping, no gap
left = self.rng[:5]
right = self.rng[5:10]
left = rng[:5]
right = rng[5:10]

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

the_union = self.rng.union(rng, sort=sort)
the_union = rng.union(rng, sort=sort)
assert isinstance(the_union, DatetimeIndex)

def test_union_not_cacheable(self, sort):
Expand Down Expand Up @@ -555,27 +553,25 @@ def test_intersection_duplicates(self, sort):


class TestCustomDatetimeIndex:
def setup_method(self):
self.rng = bdate_range(START, END, freq="C")

def test_union(self, sort):
# overlapping
left = self.rng[:10]
right = self.rng[5:10]
rng = bdate_range(START, END, freq="C")
left = rng[:10]
right = rng[5:10]

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

# non-overlapping, gap in middle
left = self.rng[:5]
right = self.rng[10:]
left = rng[:5]
right = rng[10:]

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

# non-overlapping, no gap
left = self.rng[:5]
right = self.rng[5:10]
left = rng[:5]
right = rng[5:10]

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

the_union = self.rng.union(rng, sort=sort)
the_union = rng.union(rng, sort=sort)
assert isinstance(the_union, DatetimeIndex)

def test_intersection_bug(self):
Expand Down
76 changes: 40 additions & 36 deletions pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,37 @@
from pandas.api.types import CategoricalDtype as CDT


class TestCategoricalIndex:
def setup_method(self):
@pytest.fixture
def df():
return DataFrame(
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cab")), name="B"),
)


@pytest.fixture
def df2():
return DataFrame(
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
)

self.df = DataFrame(
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cab")), name="B"),
)
self.df2 = DataFrame(
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
)

def test_loc_scalar(self):
class TestCategoricalIndex:
def test_loc_scalar(self, df):
dtype = CDT(list("cab"))
result = self.df.loc["a"]
result = df.loc["a"]
bidx = Series(list("aaa"), name="B").astype(dtype)
assert bidx.dtype == dtype

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

df = self.df.copy()
df = df.copy()
df.loc["a"] = 20
bidx2 = Series(list("aabbca"), name="B").astype(dtype)
assert bidx2.dtype == dtype
Expand All @@ -68,9 +73,8 @@ def test_loc_scalar(self):
df2.loc["d"] = 10
tm.assert_frame_equal(df2, expected)

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

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

def test_loc_getitem_scalar_non_category(self):
def test_loc_getitem_scalar_non_category(self, df):
with pytest.raises(KeyError, match="^1$"):
self.df.loc[1]
df.loc[1]

def test_slicing(self):
cat = Series(Categorical([1, 2, 3, 4]))
Expand Down Expand Up @@ -287,31 +291,31 @@ def test_slicing_doc_examples(self):
)
tm.assert_frame_equal(result, expected)

def test_loc_getitem_listlike_labels(self):
def test_loc_getitem_listlike_labels(self, df):
# list of labels
result = self.df.loc[["c", "a"]]
expected = self.df.iloc[[4, 0, 1, 5]]
result = df.loc[["c", "a"]]
expected = df.iloc[[4, 0, 1, 5]]
tm.assert_frame_equal(result, expected, check_index_type=True)

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

def test_loc_getitem_label_unused_category(self):
def test_loc_getitem_label_unused_category(self, df2):
# element in the categories but not in the values
with pytest.raises(KeyError, match=r"^'e'$"):
self.df2.loc["e"]
df2.loc["e"]

def test_loc_getitem_non_category(self):
def test_loc_getitem_non_category(self, df2):
# not all labels in the categories
with pytest.raises(KeyError, match=re.escape("['d'] not in index")):
self.df2.loc[["a", "d"]]
df2.loc[["a", "d"]]

def test_loc_setitem_expansion_label_unused_category(self):
def test_loc_setitem_expansion_label_unused_category(self, df2):
# assigning with a label that is in the categories but not in the index
df = self.df2.copy()
df = df2.copy()
df.loc["e"] = 20
result = df.loc[["a", "b", "e"]]
exp_index = CategoricalIndex(list("aaabbe"), categories=list("cabe"), name="B")
Expand Down Expand Up @@ -450,17 +454,17 @@ def test_ix_categorical_index_non_unique(self):
)
tm.assert_frame_equal(cdf.loc[:, ["X", "Y"]], expect)

def test_loc_slice(self):
def test_loc_slice(self, df):
# GH9748
msg = (
"cannot do slice indexing on CategoricalIndex with these "
r"indexers \[1\] of type int"
)
with pytest.raises(TypeError, match=msg):
self.df.loc[1:5]
df.loc[1:5]

result = self.df.loc["b":"c"]
expected = self.df.iloc[[2, 3, 4]]
result = df.loc["b":"c"]
expected = df.iloc[[2, 3, 4]]
tm.assert_frame_equal(result, expected)

def test_loc_and_at_with_categorical_index(self):
Expand Down
47 changes: 23 additions & 24 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,12 @@ def create_mgr(descr, item_shape=None):
)


class TestBlock:
def setup_method(self):
self.fblock = create_block("float", [0, 2, 4])
self.cblock = create_block("complex", [7])
self.oblock = create_block("object", [1, 3])
self.bool_block = create_block("bool", [5])
@pytest.fixture
def fblock():
return create_block("float", [0, 2, 4])


class TestBlock:
def test_constructor(self):
int32block = create_block("i4", [0])
assert int32block.dtype == np.int32
Expand All @@ -267,24 +266,24 @@ def test_pickle(self, typ, data):
blk = create_block(typ, data)
assert_block_equal(tm.round_trip_pickle(blk), blk)

def test_mgr_locs(self):
assert isinstance(self.fblock.mgr_locs, BlockPlacement)
def test_mgr_locs(self, fblock):
assert isinstance(fblock.mgr_locs, BlockPlacement)
tm.assert_numpy_array_equal(
self.fblock.mgr_locs.as_array, np.array([0, 2, 4], dtype=np.intp)
fblock.mgr_locs.as_array, np.array([0, 2, 4], dtype=np.intp)
)

def test_attrs(self):
assert self.fblock.shape == self.fblock.values.shape
assert self.fblock.dtype == self.fblock.values.dtype
assert len(self.fblock) == len(self.fblock.values)
def test_attrs(self, fblock):
assert fblock.shape == fblock.values.shape
assert fblock.dtype == fblock.values.dtype
assert len(fblock) == len(fblock.values)

def test_copy(self):
cop = self.fblock.copy()
assert cop is not self.fblock
assert_block_equal(self.fblock, cop)
def test_copy(self, fblock):
cop = fblock.copy()
assert cop is not fblock
assert_block_equal(fblock, cop)

def test_delete(self):
newb = self.fblock.copy()
def test_delete(self, fblock):
newb = fblock.copy()
locs = newb.mgr_locs
nb = newb.delete(0)
assert newb.mgr_locs is locs
Expand All @@ -297,7 +296,7 @@ def test_delete(self):
assert not (newb.values[0] == 1).all()
assert (nb.values[0] == 1).all()

newb = self.fblock.copy()
newb = fblock.copy()
locs = newb.mgr_locs
nb = newb.delete(1)
assert newb.mgr_locs is locs
Expand All @@ -308,15 +307,15 @@ def test_delete(self):
assert not (newb.values[1] == 2).all()
assert (nb.values[1] == 2).all()

newb = self.fblock.copy()
newb = fblock.copy()
locs = newb.mgr_locs
nb = newb.delete(2)
tm.assert_numpy_array_equal(
nb.mgr_locs.as_array, np.array([0, 2], dtype=np.intp)
)
assert (nb.values[1] == 1).all()

newb = self.fblock.copy()
newb = fblock.copy()

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

def test_is_categorical_deprecated(self):
def test_is_categorical_deprecated(self, fblock):
# GH#40571
blk = self.fblock
blk = fblock
with tm.assert_produces_warning(DeprecationWarning):
blk.is_categorical

Expand Down
Loading