Skip to content

TST: Use fixtures instead of setup_method #45842

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
Feb 7, 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
44 changes: 26 additions & 18 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@
)


class TestSparseArray:
def setup_method(self):
self.arr_data = np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])
self.arr = SparseArray(self.arr_data)
self.zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
@pytest.fixture
def arr_data():
return np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])


@pytest.fixture
def arr(arr_data):
return SparseArray(arr_data)


@pytest.fixture
def zarr():
return SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)


class TestSparseArray:
@pytest.mark.parametrize("fill_value", [0, None, np.nan])
def test_shift_fill_value(self, fill_value):
# GH #24128
Expand Down Expand Up @@ -79,13 +89,13 @@ def test_set_fill_invalid_non_scalar(self, val):
with pytest.raises(ValueError, match=msg):
arr.fill_value = val

def test_copy(self):
arr2 = self.arr.copy()
assert arr2.sp_values is not self.arr.sp_values
assert arr2.sp_index is self.arr.sp_index
def test_copy(self, arr):
arr2 = arr.copy()
assert arr2.sp_values is not arr.sp_values
assert arr2.sp_index is arr.sp_index

def test_values_asarray(self):
tm.assert_almost_equal(self.arr.to_dense(), self.arr_data)
def test_values_asarray(self, arr_data, arr):
tm.assert_almost_equal(arr.to_dense(), arr_data)

@pytest.mark.parametrize(
"data,shape,dtype",
Expand Down Expand Up @@ -121,13 +131,11 @@ def test_dense_repr(self, vals, fill_value):

tm.assert_numpy_array_equal(res2, vals)

def test_pickle(self):
def _check_roundtrip(obj):
unpickled = tm.round_trip_pickle(obj)
tm.assert_sp_array_equal(unpickled, obj)

_check_roundtrip(self.arr)
_check_roundtrip(self.zarr)
@pytest.mark.parametrize("fix", ["arr", "zarr"])
def test_pickle(self, fix, request):
obj = request.getfixturevalue(fix)
unpickled = tm.round_trip_pickle(obj)
tm.assert_sp_array_equal(unpickled, obj)

def test_generator_warnings(self):
sp_arr = SparseArray([1, 2, 3])
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/base/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class Delegate(PandasDelegate, PandasObject):
def __init__(self, obj):
self.obj = obj

def setup_method(self, method):
pass

def test_invalid_delegation(self):
# these show that in order for the delegation to work
# the _delegate_* methods need to be overridden to not raise
Expand Down
82 changes: 45 additions & 37 deletions pandas/tests/indexes/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
from pandas.core.indexes.frozen import FrozenList


class TestFrozenList:
@pytest.fixture
def lst():
return [1, 2, 3, 4, 5]


@pytest.fixture
def container(lst):
return FrozenList(lst)

unicode_container = FrozenList(["\u05d0", "\u05d1", "c"])

def setup_method(self, _):
self.lst = [1, 2, 3, 4, 5]
self.container = FrozenList(self.lst)
@pytest.fixture
def unicode_container():
return FrozenList(["\u05d0", "\u05d1", "c"])


class TestFrozenList:
def check_mutable_error(self, *args, **kwargs):
# Pass whatever function you normally would to pytest.raises
# (after the Exception kind).
Expand All @@ -21,75 +29,75 @@ def check_mutable_error(self, *args, **kwargs):
with pytest.raises(TypeError, match=msg):
mutable_regex(*args, **kwargs)

def test_no_mutable_funcs(self):
def test_no_mutable_funcs(self, container):
def setitem():
self.container[0] = 5
container[0] = 5

self.check_mutable_error(setitem)

def setslice():
self.container[1:2] = 3
container[1:2] = 3

self.check_mutable_error(setslice)

def delitem():
del self.container[0]
del container[0]

self.check_mutable_error(delitem)

def delslice():
del self.container[0:3]
del container[0:3]

self.check_mutable_error(delslice)

mutable_methods = ("extend", "pop", "remove", "insert")

for meth in mutable_methods:
self.check_mutable_error(getattr(self.container, meth))
self.check_mutable_error(getattr(container, meth))

def test_slicing_maintains_type(self):
result = self.container[1:2]
expected = self.lst[1:2]
def test_slicing_maintains_type(self, container, lst):
result = container[1:2]
expected = lst[1:2]
self.check_result(result, expected)

def check_result(self, result, expected):
assert isinstance(result, FrozenList)
assert result == expected

def test_string_methods_dont_fail(self):
repr(self.container)
str(self.container)
bytes(self.container)
def test_string_methods_dont_fail(self, container):
repr(container)
str(container)
bytes(container)

def test_tricky_container(self):
repr(self.unicode_container)
str(self.unicode_container)
def test_tricky_container(self, unicode_container):
repr(unicode_container)
str(unicode_container)

def test_add(self):
result = self.container + (1, 2, 3)
expected = FrozenList(self.lst + [1, 2, 3])
def test_add(self, container, lst):
result = container + (1, 2, 3)
expected = FrozenList(lst + [1, 2, 3])
self.check_result(result, expected)

result = (1, 2, 3) + self.container
expected = FrozenList([1, 2, 3] + self.lst)
result = (1, 2, 3) + container
expected = FrozenList([1, 2, 3] + lst)
self.check_result(result, expected)

def test_iadd(self):
q = r = self.container
def test_iadd(self, container, lst):
q = r = container

q += [5]
self.check_result(q, self.lst + [5])
self.check_result(q, lst + [5])

# Other shouldn't be mutated.
self.check_result(r, self.lst)
self.check_result(r, lst)

def test_union(self):
result = self.container.union((1, 2, 3))
expected = FrozenList(self.lst + [1, 2, 3])
def test_union(self, container, lst):
result = container.union((1, 2, 3))
expected = FrozenList(lst + [1, 2, 3])
self.check_result(result, expected)

def test_difference(self):
result = self.container.difference([2])
def test_difference(self, container):
result = container.difference([2])
expected = FrozenList([1, 3, 4, 5])
self.check_result(result, expected)

Expand All @@ -98,8 +106,8 @@ def test_difference_dupe(self):
expected = FrozenList([1, 3])
self.check_result(result, expected)

def test_tricky_container_to_bytes_raises(self):
def test_tricky_container_to_bytes_raises(self, unicode_container):
# GH 26447
msg = "^'str' object cannot be interpreted as an integer$"
with pytest.raises(TypeError, match=msg):
bytes(self.unicode_container)
bytes(unicode_container)
70 changes: 36 additions & 34 deletions pandas/tests/window/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ def times_frame():
)


class TestRolling:
def setup_method(self):
self.frame = DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})
@pytest.fixture
def roll_frame():
return DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})


def test_mutated(self):
class TestRolling:
def test_mutated(self, roll_frame):

msg = r"groupby\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
self.frame.groupby("A", foo=1)
roll_frame.groupby("A", foo=1)

g = self.frame.groupby("A")
g = roll_frame.groupby("A")
assert not g.mutated
g = get_groupby(self.frame, by="A", mutated=True)
g = get_groupby(roll_frame, by="A", mutated=True)
assert g.mutated

def test_getitem(self):
g = self.frame.groupby("A")
g_mutated = get_groupby(self.frame, by="A", mutated=True)
def test_getitem(self, roll_frame):
g = roll_frame.groupby("A")
g_mutated = get_groupby(roll_frame, by="A", mutated=True)

expected = g_mutated.B.apply(lambda x: x.rolling(2).mean())

Expand All @@ -70,15 +72,15 @@ def test_getitem(self):
result = g.B.rolling(2).mean()
tm.assert_series_equal(result, expected)

result = self.frame.B.groupby(self.frame.A).rolling(2).mean()
result = roll_frame.B.groupby(roll_frame.A).rolling(2).mean()
tm.assert_series_equal(result, expected)

def test_getitem_multiple(self):
def test_getitem_multiple(self, roll_frame):

# GH 13174
g = self.frame.groupby("A")
g = roll_frame.groupby("A")
r = g.rolling(2, min_periods=0)
g_mutated = get_groupby(self.frame, by="A", mutated=True)
g_mutated = get_groupby(roll_frame, by="A", mutated=True)
expected = g_mutated.B.apply(lambda x: x.rolling(2, min_periods=0).count())

result = r.B.count()
Expand All @@ -102,38 +104,38 @@ def test_getitem_multiple(self):
"skew",
],
)
def test_rolling(self, f):
g = self.frame.groupby("A")
def test_rolling(self, f, roll_frame):
g = roll_frame.groupby("A")
r = g.rolling(window=4)

result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.rolling(4), f)())
# groupby.apply doesn't drop the grouped-by column
expected = expected.drop("A", axis=1)
# GH 39732
expected_index = MultiIndex.from_arrays([self.frame["A"], range(40)])
expected_index = MultiIndex.from_arrays([roll_frame["A"], range(40)])
expected.index = expected_index
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["std", "var"])
def test_rolling_ddof(self, f):
g = self.frame.groupby("A")
def test_rolling_ddof(self, f, roll_frame):
g = roll_frame.groupby("A")
r = g.rolling(window=4)

result = getattr(r, f)(ddof=1)
expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1))
# groupby.apply doesn't drop the grouped-by column
expected = expected.drop("A", axis=1)
# GH 39732
expected_index = MultiIndex.from_arrays([self.frame["A"], range(40)])
expected_index = MultiIndex.from_arrays([roll_frame["A"], range(40)])
expected.index = expected_index
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"interpolation", ["linear", "lower", "higher", "midpoint", "nearest"]
)
def test_rolling_quantile(self, interpolation):
g = self.frame.groupby("A")
def test_rolling_quantile(self, interpolation, roll_frame):
g = roll_frame.groupby("A")
r = g.rolling(window=4)

result = r.quantile(0.4, interpolation=interpolation)
Expand All @@ -143,7 +145,7 @@ def test_rolling_quantile(self, interpolation):
# groupby.apply doesn't drop the grouped-by column
expected = expected.drop("A", axis=1)
# GH 39732
expected_index = MultiIndex.from_arrays([self.frame["A"], range(40)])
expected_index = MultiIndex.from_arrays([roll_frame["A"], range(40)])
expected.index = expected_index
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -173,14 +175,14 @@ def test_rolling_corr_cov_other_same_size_as_groups(self, f, expected_val):
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["corr", "cov"])
def test_rolling_corr_cov_other_diff_size_as_groups(self, f):
g = self.frame.groupby("A")
def test_rolling_corr_cov_other_diff_size_as_groups(self, f, roll_frame):
g = roll_frame.groupby("A")
r = g.rolling(window=4)

result = getattr(r, f)(self.frame)
result = getattr(r, f)(roll_frame)

def func(x):
return getattr(x.rolling(4), f)(self.frame)
return getattr(x.rolling(4), f)(roll_frame)

expected = g.apply(func)
# GH 39591: The grouped column should be all np.nan
Expand All @@ -189,8 +191,8 @@ def func(x):
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["corr", "cov"])
def test_rolling_corr_cov_pairwise(self, f):
g = self.frame.groupby("A")
def test_rolling_corr_cov_pairwise(self, f, roll_frame):
g = roll_frame.groupby("A")
r = g.rolling(window=4)

result = getattr(r.B, f)(pairwise=True)
Expand Down Expand Up @@ -237,8 +239,8 @@ def test_rolling_corr_cov_unordered(self, func, expected_values):
)
tm.assert_frame_equal(result, expected)

def test_rolling_apply(self, raw):
g = self.frame.groupby("A")
def test_rolling_apply(self, raw, roll_frame):
g = roll_frame.groupby("A")
r = g.rolling(window=4)

# reduction
Expand All @@ -247,7 +249,7 @@ def test_rolling_apply(self, raw):
# groupby.apply doesn't drop the grouped-by column
expected = expected.drop("A", axis=1)
# GH 39732
expected_index = MultiIndex.from_arrays([self.frame["A"], range(40)])
expected_index = MultiIndex.from_arrays([roll_frame["A"], range(40)])
expected.index = expected_index
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -778,9 +780,9 @@ def test_groupby_rolling_resulting_multiindex3(self):
)
tm.assert_index_equal(result.index, expected_index, exact="equiv")

def test_groupby_rolling_object_doesnt_affect_groupby_apply(self):
def test_groupby_rolling_object_doesnt_affect_groupby_apply(self, roll_frame):
# GH 39732
g = self.frame.groupby("A")
g = roll_frame.groupby("A")
expected = g.apply(lambda x: x.rolling(4).sum()).index
_ = g.rolling(window=4)
result = g.apply(lambda x: x.rolling(4).sum()).index
Expand Down
Loading