20
20
def rolling_dfs (scalars_dfs ):
21
21
bf_df , pd_df = scalars_dfs
22
22
23
- target_cols = ["int64_too" , "float64_col" , "bool_col " ]
23
+ target_cols = ["int64_too" , "float64_col" , "int64_col " ]
24
24
25
- bf_df = bf_df [target_cols ].set_index ("bool_col" )
26
- pd_df = pd_df [target_cols ].set_index ("bool_col" )
27
-
28
- return bf_df , pd_df
25
+ return bf_df [target_cols ], pd_df [target_cols ]
29
26
30
27
31
28
@pytest .fixture (scope = "module" )
@@ -49,31 +46,65 @@ def test_dataframe_rolling_closed_param(rolling_dfs, closed):
49
46
@pytest .mark .parametrize ("closed" , ["left" , "right" , "both" , "neither" ])
50
47
def test_dataframe_groupby_rolling_closed_param (rolling_dfs , closed ):
51
48
bf_df , pd_df = rolling_dfs
49
+ # Need to specify column subset for comparison due to b/406841327
50
+ check_columns = ["float64_col" , "int64_col" ]
52
51
53
52
actual_result = (
54
- bf_df .groupby (level = 0 ).rolling (window = 3 , closed = closed ).sum ().to_pandas ()
53
+ bf_df .groupby (bf_df ["int64_too" ] % 2 )
54
+ .rolling (window = 3 , closed = closed )
55
+ .sum ()
56
+ .to_pandas ()
55
57
)
56
58
57
- expected_result = pd_df .groupby (level = 0 ).rolling (window = 3 , closed = closed ).sum ()
58
- pd .testing .assert_frame_equal (actual_result , expected_result , check_dtype = False )
59
+ expected_result = (
60
+ pd_df .groupby (pd_df ["int64_too" ] % 2 ).rolling (window = 3 , closed = closed ).sum ()
61
+ )
62
+ pd .testing .assert_frame_equal (
63
+ actual_result [check_columns ], expected_result , check_dtype = False
64
+ )
59
65
60
66
61
- def test_dataframe_rolling_default_closed_param (rolling_dfs ):
67
+ def test_dataframe_rolling_on (rolling_dfs ):
62
68
bf_df , pd_df = rolling_dfs
63
69
64
- actual_result = bf_df .rolling (window = 3 ).sum ().to_pandas ()
70
+ actual_result = bf_df .rolling (window = 3 , on = "int64_too" ).sum ().to_pandas ()
65
71
66
- expected_result = pd_df .rolling (window = 3 ).sum ()
72
+ expected_result = pd_df .rolling (window = 3 , on = "int64_too" ).sum ()
67
73
pd .testing .assert_frame_equal (actual_result , expected_result , check_dtype = False )
68
74
69
75
70
- def test_dataframe_groupby_rolling_default_closed_param (rolling_dfs ):
76
+ def test_dataframe_rolling_on_invalid_column_raise_error (rolling_dfs ):
77
+ bf_df , _ = rolling_dfs
78
+
79
+ with pytest .raises (ValueError ):
80
+ bf_df .rolling (window = 3 , on = "whatever" ).sum ()
81
+
82
+
83
+ def test_dataframe_groupby_rolling_on (rolling_dfs ):
71
84
bf_df , pd_df = rolling_dfs
85
+ # Need to specify column subset for comparison due to b/406841327
86
+ check_columns = ["float64_col" , "int64_col" ]
72
87
73
- actual_result = bf_df .groupby (level = 0 ).rolling (window = 3 ).sum ().to_pandas ()
88
+ actual_result = (
89
+ bf_df .groupby (bf_df ["int64_too" ] % 2 )
90
+ .rolling (window = 3 , on = "float64_col" )
91
+ .sum ()
92
+ .to_pandas ()
93
+ )
74
94
75
- expected_result = pd_df .groupby (level = 0 ).rolling (window = 3 ).sum ()
76
- pd .testing .assert_frame_equal (actual_result , expected_result , check_dtype = False )
95
+ expected_result = (
96
+ pd_df .groupby (pd_df ["int64_too" ] % 2 ).rolling (window = 3 , on = "float64_col" ).sum ()
97
+ )
98
+ pd .testing .assert_frame_equal (
99
+ actual_result [check_columns ], expected_result , check_dtype = False
100
+ )
101
+
102
+
103
+ def test_dataframe_groupby_rolling_on_invalid_column_raise_error (rolling_dfs ):
104
+ bf_df , _ = rolling_dfs
105
+
106
+ with pytest .raises (ValueError ):
107
+ bf_df .groupby (level = 0 ).rolling (window = 3 , on = "whatever" ).sum ()
77
108
78
109
79
110
@pytest .mark .parametrize ("closed" , ["left" , "right" , "both" , "neither" ])
@@ -103,24 +134,6 @@ def test_series_groupby_rolling_closed_param(rolling_series, closed):
103
134
pd .testing .assert_series_equal (actual_result , expected_result , check_dtype = False )
104
135
105
136
106
- def test_series_rolling_default_closed_param (rolling_series ):
107
- bf_series , df_series = rolling_series
108
-
109
- actual_result = bf_series .rolling (window = 3 ).sum ().to_pandas ()
110
-
111
- expected_result = df_series .rolling (window = 3 ).sum ()
112
- pd .testing .assert_series_equal (actual_result , expected_result , check_dtype = False )
113
-
114
-
115
- def test_series_groupby_rolling_default_closed_param (rolling_series ):
116
- bf_series , df_series = rolling_series
117
-
118
- actual_result = bf_series .groupby (bf_series % 2 ).rolling (window = 3 ).sum ().to_pandas ()
119
-
120
- expected_result = df_series .groupby (df_series % 2 ).rolling (window = 3 ).sum ()
121
- pd .testing .assert_series_equal (actual_result , expected_result , check_dtype = False )
122
-
123
-
124
137
@pytest .mark .parametrize (
125
138
("windowing" ),
126
139
[
@@ -181,8 +194,12 @@ def test_series_window_agg_ops(rolling_series, windowing, agg_op):
181
194
pytest .param (lambda x : x .var (), id = "var" ),
182
195
],
183
196
)
184
- def test_dataframe_window_agg_ops (rolling_dfs , windowing , agg_op ):
185
- bf_df , pd_df = rolling_dfs
197
+ def test_dataframe_window_agg_ops (scalars_dfs , windowing , agg_op ):
198
+ bf_df , pd_df = scalars_dfs
199
+ target_columns = ["int64_too" , "float64_col" , "bool_col" ]
200
+ index_column = "bool_col"
201
+ bf_df = bf_df [target_columns ].set_index (index_column )
202
+ pd_df = pd_df [target_columns ].set_index (index_column )
186
203
187
204
bf_result = agg_op (windowing (bf_df )).to_pandas ()
188
205
0 commit comments