@@ -122,14 +122,37 @@ def test_numpy_compat(method):
122
122
getattr (r , method )(dtype = np .float64 )
123
123
124
124
125
- def test_closed ():
126
- df = DataFrame ({"A" : [0 , 1 , 2 , 3 , 4 ]})
127
- # closed only allowed for datetimelike
125
+ @pytest .mark .parametrize ("closed" , ["left" , "right" , "both" , "neither" ])
126
+ def test_closed_fixed (closed , arithmetic_win_operators ):
127
+ # GH 34315
128
+ func_name = arithmetic_win_operators
129
+ df_fixed = DataFrame ({"A" : [0 , 1 , 2 , 3 , 4 ]})
130
+ df_time = DataFrame ({"A" : [0 , 1 , 2 , 3 , 4 ]}, index = date_range ("2020" , periods = 5 ))
128
131
129
- msg = "closed only implemented for datetimelike and offset based windows"
132
+ result = getattr (df_fixed .rolling (2 , closed = closed , min_periods = 1 ), func_name )()
133
+ expected = getattr (df_time .rolling ("2D" , closed = closed ), func_name )().reset_index (
134
+ drop = True
135
+ )
130
136
131
- with pytest .raises (ValueError , match = msg ):
132
- df .rolling (window = 3 , closed = "neither" )
137
+ tm .assert_frame_equal (result , expected )
138
+
139
+
140
+ def test_closed_fixed_binary_col ():
141
+ # GH 34315
142
+ data = [0 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ]
143
+ df = DataFrame (
144
+ {"binary_col" : data },
145
+ index = pd .date_range (start = "2020-01-01" , freq = "min" , periods = len (data )),
146
+ )
147
+
148
+ rolling = df .rolling (window = len (df ), closed = "left" , min_periods = 1 )
149
+ result = rolling .mean ()
150
+ expected = DataFrame (
151
+ [np .nan , 0 , 0.5 , 2 / 3 , 0.5 , 0.4 , 0.5 , 0.428571 ],
152
+ columns = ["binary_col" ],
153
+ index = pd .date_range (start = "2020-01-01" , freq = "min" , periods = len (data )),
154
+ )
155
+ tm .assert_frame_equal (result , expected )
133
156
134
157
135
158
@pytest .mark .parametrize ("closed" , ["neither" , "left" ])
0 commit comments