@@ -40,24 +40,26 @@ def times_frame():
40
40
)
41
41
42
42
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
+
46
47
47
- def test_mutated (self ):
48
+ class TestRolling :
49
+ def test_mutated (self , roll_frame ):
48
50
49
51
msg = r"groupby\(\) got an unexpected keyword argument 'foo'"
50
52
with pytest .raises (TypeError , match = msg ):
51
- self . frame .groupby ("A" , foo = 1 )
53
+ roll_frame .groupby ("A" , foo = 1 )
52
54
53
- g = self . frame .groupby ("A" )
55
+ g = roll_frame .groupby ("A" )
54
56
assert not g .mutated
55
- g = get_groupby (self . frame , by = "A" , mutated = True )
57
+ g = get_groupby (roll_frame , by = "A" , mutated = True )
56
58
assert g .mutated
57
59
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 )
61
63
62
64
expected = g_mutated .B .apply (lambda x : x .rolling (2 ).mean ())
63
65
@@ -70,15 +72,15 @@ def test_getitem(self):
70
72
result = g .B .rolling (2 ).mean ()
71
73
tm .assert_series_equal (result , expected )
72
74
73
- result = self . frame . B .groupby (self . frame .A ).rolling (2 ).mean ()
75
+ result = roll_frame . B .groupby (roll_frame .A ).rolling (2 ).mean ()
74
76
tm .assert_series_equal (result , expected )
75
77
76
- def test_getitem_multiple (self ):
78
+ def test_getitem_multiple (self , roll_frame ):
77
79
78
80
# GH 13174
79
- g = self . frame .groupby ("A" )
81
+ g = roll_frame .groupby ("A" )
80
82
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 )
82
84
expected = g_mutated .B .apply (lambda x : x .rolling (2 , min_periods = 0 ).count ())
83
85
84
86
result = r .B .count ()
@@ -102,38 +104,38 @@ def test_getitem_multiple(self):
102
104
"skew" ,
103
105
],
104
106
)
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" )
107
109
r = g .rolling (window = 4 )
108
110
109
111
result = getattr (r , f )()
110
112
expected = g .apply (lambda x : getattr (x .rolling (4 ), f )())
111
113
# groupby.apply doesn't drop the grouped-by column
112
114
expected = expected .drop ("A" , axis = 1 )
113
115
# GH 39732
114
- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
116
+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
115
117
expected .index = expected_index
116
118
tm .assert_frame_equal (result , expected )
117
119
118
120
@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" )
121
123
r = g .rolling (window = 4 )
122
124
123
125
result = getattr (r , f )(ddof = 1 )
124
126
expected = g .apply (lambda x : getattr (x .rolling (4 ), f )(ddof = 1 ))
125
127
# groupby.apply doesn't drop the grouped-by column
126
128
expected = expected .drop ("A" , axis = 1 )
127
129
# GH 39732
128
- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
130
+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
129
131
expected .index = expected_index
130
132
tm .assert_frame_equal (result , expected )
131
133
132
134
@pytest .mark .parametrize (
133
135
"interpolation" , ["linear" , "lower" , "higher" , "midpoint" , "nearest" ]
134
136
)
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" )
137
139
r = g .rolling (window = 4 )
138
140
139
141
result = r .quantile (0.4 , interpolation = interpolation )
@@ -143,7 +145,7 @@ def test_rolling_quantile(self, interpolation):
143
145
# groupby.apply doesn't drop the grouped-by column
144
146
expected = expected .drop ("A" , axis = 1 )
145
147
# GH 39732
146
- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
148
+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
147
149
expected .index = expected_index
148
150
tm .assert_frame_equal (result , expected )
149
151
@@ -173,14 +175,14 @@ def test_rolling_corr_cov_other_same_size_as_groups(self, f, expected_val):
173
175
tm .assert_frame_equal (result , expected )
174
176
175
177
@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" )
178
180
r = g .rolling (window = 4 )
179
181
180
- result = getattr (r , f )(self . frame )
182
+ result = getattr (r , f )(roll_frame )
181
183
182
184
def func (x ):
183
- return getattr (x .rolling (4 ), f )(self . frame )
185
+ return getattr (x .rolling (4 ), f )(roll_frame )
184
186
185
187
expected = g .apply (func )
186
188
# GH 39591: The grouped column should be all np.nan
@@ -189,8 +191,8 @@ def func(x):
189
191
tm .assert_frame_equal (result , expected )
190
192
191
193
@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" )
194
196
r = g .rolling (window = 4 )
195
197
196
198
result = getattr (r .B , f )(pairwise = True )
@@ -237,8 +239,8 @@ def test_rolling_corr_cov_unordered(self, func, expected_values):
237
239
)
238
240
tm .assert_frame_equal (result , expected )
239
241
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" )
242
244
r = g .rolling (window = 4 )
243
245
244
246
# reduction
@@ -247,7 +249,7 @@ def test_rolling_apply(self, raw):
247
249
# groupby.apply doesn't drop the grouped-by column
248
250
expected = expected .drop ("A" , axis = 1 )
249
251
# GH 39732
250
- expected_index = MultiIndex .from_arrays ([self . frame ["A" ], range (40 )])
252
+ expected_index = MultiIndex .from_arrays ([roll_frame ["A" ], range (40 )])
251
253
expected .index = expected_index
252
254
tm .assert_frame_equal (result , expected )
253
255
@@ -778,9 +780,9 @@ def test_groupby_rolling_resulting_multiindex3(self):
778
780
)
779
781
tm .assert_index_equal (result .index , expected_index , exact = "equiv" )
780
782
781
- def test_groupby_rolling_object_doesnt_affect_groupby_apply (self ):
783
+ def test_groupby_rolling_object_doesnt_affect_groupby_apply (self , roll_frame ):
782
784
# GH 39732
783
- g = self . frame .groupby ("A" )
785
+ g = roll_frame .groupby ("A" )
784
786
expected = g .apply (lambda x : x .rolling (4 ).sum ()).index
785
787
_ = g .rolling (window = 4 )
786
788
result = g .apply (lambda x : x .rolling (4 ).sum ()).index
0 commit comments