diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 0e734f0c1a1e8..50a56761eda8c 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -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 @@ -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", @@ -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]) diff --git a/pandas/tests/base/test_constructors.py b/pandas/tests/base/test_constructors.py index 16ce709a5b021..c845be1d0d1f5 100644 --- a/pandas/tests/base/test_constructors.py +++ b/pandas/tests/base/test_constructors.py @@ -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 diff --git a/pandas/tests/indexes/test_frozen.py b/pandas/tests/indexes/test_frozen.py index cde3fc00eaaaa..ace66b5b06a51 100644 --- a/pandas/tests/indexes/test_frozen.py +++ b/pandas/tests/indexes/test_frozen.py @@ -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). @@ -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) @@ -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) diff --git a/pandas/tests/window/test_groupby.py b/pandas/tests/window/test_groupby.py index 5125587df9ea2..a1e9ad2677777 100644 --- a/pandas/tests/window/test_groupby.py +++ b/pandas/tests/window/test_groupby.py @@ -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()) @@ -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() @@ -102,8 +104,8 @@ 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)() @@ -111,13 +113,13 @@ def test_rolling(self, 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) @@ -125,15 +127,15 @@ def test_rolling_ddof(self, 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( "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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index f51764e04715a..41152c0495494 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -15,26 +15,31 @@ import pandas.tseries.offsets as offsets +@pytest.fixture +def regular(): + return DataFrame( + {"A": date_range("20130101", periods=5, freq="s"), "B": range(5)} + ).set_index("A") + + +@pytest.fixture +def ragged(): + df = DataFrame({"B": range(5)}) + df.index = [ + Timestamp("20130101 09:00:00"), + Timestamp("20130101 09:00:02"), + Timestamp("20130101 09:00:03"), + Timestamp("20130101 09:00:05"), + Timestamp("20130101 09:00:06"), + ] + return df + + class TestRollingTS: # rolling time-series friendly # xref GH13327 - def setup_method(self): - - self.regular = DataFrame( - {"A": date_range("20130101", periods=5, freq="s"), "B": range(5)} - ).set_index("A") - - self.ragged = DataFrame({"B": range(5)}) - self.ragged.index = [ - Timestamp("20130101 09:00:00"), - Timestamp("20130101 09:00:02"), - Timestamp("20130101 09:00:03"), - Timestamp("20130101 09:00:05"), - Timestamp("20130101 09:00:06"), - ] - def test_doc_string(self): df = DataFrame( @@ -50,42 +55,42 @@ def test_doc_string(self): df df.rolling("2s").sum() - def test_invalid_window_non_int(self): + def test_invalid_window_non_int(self, regular): # not a valid freq msg = "passed window foobar is not compatible with a datetimelike index" with pytest.raises(ValueError, match=msg): - self.regular.rolling(window="foobar") + regular.rolling(window="foobar") # not a datetimelike index msg = "window must be an integer" with pytest.raises(ValueError, match=msg): - self.regular.reset_index().rolling(window="foobar") + regular.reset_index().rolling(window="foobar") @pytest.mark.parametrize("freq", ["2MS", offsets.MonthBegin(2)]) - def test_invalid_window_nonfixed(self, freq): + def test_invalid_window_nonfixed(self, freq, regular): # non-fixed freqs msg = "\\<2 \\* MonthBegins\\> is a non-fixed frequency" with pytest.raises(ValueError, match=msg): - self.regular.rolling(window=freq) + regular.rolling(window=freq) @pytest.mark.parametrize("freq", ["1D", offsets.Day(2), "2ms"]) - def test_valid_window(self, freq): - self.regular.rolling(window=freq) + def test_valid_window(self, freq, regular): + regular.rolling(window=freq) @pytest.mark.parametrize("minp", [1.0, "foo", np.array([1, 2, 3])]) - def test_invalid_minp(self, minp): + def test_invalid_minp(self, minp, regular): # non-integer min_periods msg = ( r"local variable 'minp' referenced before assignment|" "min_periods must be an integer" ) with pytest.raises(ValueError, match=msg): - self.regular.rolling(window="1D", min_periods=minp) + regular.rolling(window="1D", min_periods=minp) - def test_on(self): + def test_on(self, regular): - df = self.regular + df = regular # not a valid column msg = ( @@ -217,9 +222,9 @@ def test_frame_on2(self): result = df.rolling("2s", on="C")[["A", "B", "C"]].sum() tm.assert_frame_equal(result, expected) - def test_basic_regular(self): + def test_basic_regular(self, regular): - df = self.regular.copy() + df = regular.copy() df.index = date_range("20130101", periods=5, freq="D") expected = df.rolling(window=1, min_periods=1).sum() @@ -239,10 +244,10 @@ def test_basic_regular(self): result = df.rolling(window="2D").sum() tm.assert_frame_equal(result, expected) - def test_min_periods(self): + def test_min_periods(self, regular): # compare for min_periods - df = self.regular + df = regular # these slightly different expected = df.rolling(2, min_periods=1).sum() @@ -253,7 +258,7 @@ def test_min_periods(self): result = df.rolling("2s", min_periods=1).sum() tm.assert_frame_equal(result, expected) - def test_closed(self): + def test_closed(self, regular): # xref GH13965 @@ -271,7 +276,7 @@ def test_closed(self): # closed must be 'right', 'left', 'both', 'neither' msg = "closed must be 'right', 'left', 'both' or 'neither'" with pytest.raises(ValueError, match=msg): - self.regular.rolling(window="2s", closed="blabla") + regular.rolling(window="2s", closed="blabla") expected = df.copy() expected["A"] = [1.0, 2, 2, 2, 1] @@ -297,9 +302,9 @@ def test_closed(self): result = df.rolling("2s", closed="neither").sum() tm.assert_frame_equal(result, expected) - def test_ragged_sum(self): + def test_ragged_sum(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).sum() expected = df.copy() expected["B"] = [0.0, 1, 2, 3, 4] @@ -340,9 +345,9 @@ def test_ragged_sum(self): expected["B"] = [0.0, 1, 3, 6, 10] tm.assert_frame_equal(result, expected) - def test_ragged_mean(self): + def test_ragged_mean(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).mean() expected = df.copy() expected["B"] = [0.0, 1, 2, 3, 4] @@ -353,9 +358,9 @@ def test_ragged_mean(self): expected["B"] = [0.0, 1, 1.5, 3.0, 3.5] tm.assert_frame_equal(result, expected) - def test_ragged_median(self): + def test_ragged_median(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).median() expected = df.copy() expected["B"] = [0.0, 1, 2, 3, 4] @@ -366,9 +371,9 @@ def test_ragged_median(self): expected["B"] = [0.0, 1, 1.5, 3.0, 3.5] tm.assert_frame_equal(result, expected) - def test_ragged_quantile(self): + def test_ragged_quantile(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).quantile(0.5) expected = df.copy() expected["B"] = [0.0, 1, 2, 3, 4] @@ -379,9 +384,9 @@ def test_ragged_quantile(self): expected["B"] = [0.0, 1, 1.5, 3.0, 3.5] tm.assert_frame_equal(result, expected) - def test_ragged_std(self): + def test_ragged_std(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).std(ddof=0) expected = df.copy() expected["B"] = [0.0] * 5 @@ -402,9 +407,9 @@ def test_ragged_std(self): expected["B"] = [np.nan, 0.707107, 1.0, 1.0, 1.290994] tm.assert_frame_equal(result, expected) - def test_ragged_var(self): + def test_ragged_var(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).var(ddof=0) expected = df.copy() expected["B"] = [0.0] * 5 @@ -425,9 +430,9 @@ def test_ragged_var(self): expected["B"] = [np.nan, 0.5, 1.0, 1.0, 1 + 2 / 3.0] tm.assert_frame_equal(result, expected) - def test_ragged_skew(self): + def test_ragged_skew(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="3s", min_periods=1).skew() expected = df.copy() expected["B"] = [np.nan] * 5 @@ -438,9 +443,9 @@ def test_ragged_skew(self): expected["B"] = [np.nan] * 2 + [0.0, 0.0, 0.0] tm.assert_frame_equal(result, expected) - def test_ragged_kurt(self): + def test_ragged_kurt(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="3s", min_periods=1).kurt() expected = df.copy() expected["B"] = [np.nan] * 5 @@ -451,15 +456,15 @@ def test_ragged_kurt(self): expected["B"] = [np.nan] * 4 + [-1.2] tm.assert_frame_equal(result, expected) - def test_ragged_count(self): + def test_ragged_count(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).count() expected = df.copy() expected["B"] = [1.0, 1, 1, 1, 1] tm.assert_frame_equal(result, expected) - df = self.ragged + df = ragged result = df.rolling(window="1s").count() tm.assert_frame_equal(result, expected) @@ -498,9 +503,9 @@ def test_regular_min(self): expected["B"] = [5.0, 4, 3, 3, 3] tm.assert_frame_equal(result, expected) - def test_ragged_min(self): + def test_ragged_min(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).min() expected = df.copy() @@ -532,9 +537,9 @@ def test_perf_min(self): result = dfp.rolling("200s").min() assert ((result - expected) < 0.01).all().bool() - def test_ragged_max(self): + def test_ragged_max(self, ragged): - df = self.ragged + df = ragged result = df.rolling(window="1s", min_periods=1).max() expected = df.copy() @@ -600,10 +605,10 @@ def test_freqs_ops(self, freq, op, result_data): "max", ], ) - def test_all(self, f): + def test_all(self, f, regular): # simple comparison of integer vs time-based windowing - df = self.regular * 2 + df = regular * 2 er = df.rolling(window=1) r = df.rolling(window="1s")