@@ -40,24 +40,26 @@ def times_frame():
4040 )
4141
4242
43- class TestRolling :
44- def setup_method (self ):
45- self .frame = DataFrame ({"A" : [1 ] * 20 + [2 ] * 12 + [3 ] * 8 , "B" : np .arange (40 )})
43+ @pytest .fixture
44+ def roll_frame ():
45+ return DataFrame ({"A" : [1 ] * 20 + [2 ] * 12 + [3 ] * 8 , "B" : np .arange (40 )})
46+
4647
47- def test_mutated (self ):
48+ class TestRolling :
49+ def test_mutated (self , roll_frame ):
4850
4951 msg = r"groupby\(\) got an unexpected keyword argument 'foo'"
5052 with pytest .raises (TypeError , match = msg ):
51- self . frame .groupby ("A" , foo = 1 )
53+ roll_frame .groupby ("A" , foo = 1 )
5254
53- g = self . frame .groupby ("A" )
55+ g = roll_frame .groupby ("A" )
5456 assert not g .mutated
55- g = get_groupby (self . frame , by = "A" , mutated = True )
57+ g = get_groupby (roll_frame , by = "A" , mutated = True )
5658 assert g .mutated
5759
58- def test_getitem (self ):
59- g = self . frame .groupby ("A" )
60- g_mutated = get_groupby (self . frame , by = "A" , mutated = True )
60+ def test_getitem (self , roll_frame ):
61+ g = roll_frame .groupby ("A" )
62+ g_mutated = get_groupby (roll_frame , by = "A" , mutated = True )
6163
6264 expected = g_mutated .B .apply (lambda x : x .rolling (2 ).mean ())
6365
@@ -70,15 +72,15 @@ def test_getitem(self):
7072 result = g .B .rolling (2 ).mean ()
7173 tm .assert_series_equal (result , expected )
7274
73- result = self . frame . B .groupby (self . frame .A ).rolling (2 ).mean ()
75+ result = roll_frame . B .groupby (roll_frame .A ).rolling (2 ).mean ()
7476 tm .assert_series_equal (result , expected )
7577
76- def test_getitem_multiple (self ):
78+ def test_getitem_multiple (self , roll_frame ):
7779
7880 # GH 13174
79- g = self . frame .groupby ("A" )
81+ g = roll_frame .groupby ("A" )
8082 r = g .rolling (2 , min_periods = 0 )
81- g_mutated = get_groupby (self . frame , by = "A" , mutated = True )
83+ g_mutated = get_groupby (roll_frame , by = "A" , mutated = True )
8284 expected = g_mutated .B .apply (lambda x : x .rolling (2 , min_periods = 0 ).count ())
8385
8486 result = r .B .count ()
@@ -102,38 +104,38 @@ def test_getitem_multiple(self):
102104 "skew" ,
103105 ],
104106 )
105- def test_rolling (self , f ):
106- g = self . frame .groupby ("A" )
107+ def test_rolling (self , f , roll_frame ):
108+ g = roll_frame .groupby ("A" )
107109 r = g .rolling (window = 4 )
108110
109111 result = getattr (r , f )()
110112 expected = g .apply (lambda x : getattr (x .rolling (4 ), f )())
111113 # groupby.apply doesn't drop the grouped-by column
112114 expected = expected .drop ("A" , axis = 1 )
113115 # GH 39732
114- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
116+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
115117 expected .index = expected_index
116118 tm .assert_frame_equal (result , expected )
117119
118120 @pytest .mark .parametrize ("f" , ["std" , "var" ])
119- def test_rolling_ddof (self , f ):
120- g = self . frame .groupby ("A" )
121+ def test_rolling_ddof (self , f , roll_frame ):
122+ g = roll_frame .groupby ("A" )
121123 r = g .rolling (window = 4 )
122124
123125 result = getattr (r , f )(ddof = 1 )
124126 expected = g .apply (lambda x : getattr (x .rolling (4 ), f )(ddof = 1 ))
125127 # groupby.apply doesn't drop the grouped-by column
126128 expected = expected .drop ("A" , axis = 1 )
127129 # GH 39732
128- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
130+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
129131 expected .index = expected_index
130132 tm .assert_frame_equal (result , expected )
131133
132134 @pytest .mark .parametrize (
133135 "interpolation" , ["linear" , "lower" , "higher" , "midpoint" , "nearest" ]
134136 )
135- def test_rolling_quantile (self , interpolation ):
136- g = self . frame .groupby ("A" )
137+ def test_rolling_quantile (self , interpolation , roll_frame ):
138+ g = roll_frame .groupby ("A" )
137139 r = g .rolling (window = 4 )
138140
139141 result = r .quantile (0.4 , interpolation = interpolation )
@@ -143,7 +145,7 @@ def test_rolling_quantile(self, interpolation):
143145 # groupby.apply doesn't drop the grouped-by column
144146 expected = expected .drop ("A" , axis = 1 )
145147 # GH 39732
146- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
148+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
147149 expected .index = expected_index
148150 tm .assert_frame_equal (result , expected )
149151
@@ -173,14 +175,14 @@ def test_rolling_corr_cov_other_same_size_as_groups(self, f, expected_val):
173175 tm .assert_frame_equal (result , expected )
174176
175177 @pytest .mark .parametrize ("f" , ["corr" , "cov" ])
176- def test_rolling_corr_cov_other_diff_size_as_groups (self , f ):
177- g = self . frame .groupby ("A" )
178+ def test_rolling_corr_cov_other_diff_size_as_groups (self , f , roll_frame ):
179+ g = roll_frame .groupby ("A" )
178180 r = g .rolling (window = 4 )
179181
180- result = getattr (r , f )(self . frame )
182+ result = getattr (r , f )(roll_frame )
181183
182184 def func (x ):
183- return getattr (x .rolling (4 ), f )(self . frame )
185+ return getattr (x .rolling (4 ), f )(roll_frame )
184186
185187 expected = g .apply (func )
186188 # GH 39591: The grouped column should be all np.nan
@@ -189,8 +191,8 @@ def func(x):
189191 tm .assert_frame_equal (result , expected )
190192
191193 @pytest .mark .parametrize ("f" , ["corr" , "cov" ])
192- def test_rolling_corr_cov_pairwise (self , f ):
193- g = self . frame .groupby ("A" )
194+ def test_rolling_corr_cov_pairwise (self , f , roll_frame ):
195+ g = roll_frame .groupby ("A" )
194196 r = g .rolling (window = 4 )
195197
196198 result = getattr (r .B , f )(pairwise = True )
@@ -237,8 +239,8 @@ def test_rolling_corr_cov_unordered(self, func, expected_values):
237239 )
238240 tm .assert_frame_equal (result , expected )
239241
240- def test_rolling_apply (self , raw ):
241- g = self . frame .groupby ("A" )
242+ def test_rolling_apply (self , raw , roll_frame ):
243+ g = roll_frame .groupby ("A" )
242244 r = g .rolling (window = 4 )
243245
244246 # reduction
@@ -247,7 +249,7 @@ def test_rolling_apply(self, raw):
247249 # groupby.apply doesn't drop the grouped-by column
248250 expected = expected .drop ("A" , axis = 1 )
249251 # GH 39732
250- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
252+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
251253 expected .index = expected_index
252254 tm .assert_frame_equal (result , expected )
253255
@@ -778,9 +780,9 @@ def test_groupby_rolling_resulting_multiindex3(self):
778780 )
779781 tm .assert_index_equal (result .index , expected_index , exact = "equiv" )
780782
781- def test_groupby_rolling_object_doesnt_affect_groupby_apply (self ):
783+ def test_groupby_rolling_object_doesnt_affect_groupby_apply (self , roll_frame ):
782784 # GH 39732
783- g = self . frame .groupby ("A" )
785+ g = roll_frame .groupby ("A" )
784786 expected = g .apply (lambda x : x .rolling (4 ).sum ()).index
785787 _ = g .rolling (window = 4 )
786788 result = g .apply (lambda x : x .rolling (4 ).sum ()).index
0 commit comments