|
5 | 5 |
|
6 | 6 | import numpy as np
|
7 | 7 | from numpy.lib.stride_tricks import sliding_window_view
|
8 |
| - |
9 |
| - |
10 | 8 | import pytest
|
11 | 9 |
|
12 | 10 | from pandas.compat import (
|
13 | 11 | IS64,
|
14 |
| - is_platform_arm, |
15 |
| - is_platform_power, |
16 |
| - is_platform_riscv64, |
17 | 12 | )
|
18 | 13 | from pandas.errors import Pandas4Warning
|
19 | 14 |
|
@@ -1084,48 +1079,136 @@ def test_rolling_sem(frame_or_series):
|
1084 | 1079 | expected = Series([np.nan] + [0.7071067811865476] * 2)
|
1085 | 1080 | tm.assert_series_equal(result, expected)
|
1086 | 1081 |
|
| 1082 | + |
1087 | 1083 | @pytest.mark.parametrize(
|
1088 |
| - ("func", "values", "window", "ddof", "exp_value"), |
1089 |
| - [ |
1090 |
| - ("var", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 3, 1, "numpy_compute"), |
1091 |
| - ("std", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 3, 1, "numpy_compute"), |
1092 |
| - ("var", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 2, 1, "numpy_compute"), |
1093 |
| - ("std", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 2, 1, "numpy_compute"), |
1094 |
| - ("var", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0), |
1095 |
| - ("std", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0), |
1096 |
| - ("var", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0), |
1097 |
| - ("var", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0), |
1098 |
| - ("var", [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 5, 0, "numpy_compute"), |
1099 |
| - ], |
| 1084 | + ("func", "values", "window", "ddof", "exp_value"), |
| 1085 | + [ |
| 1086 | + ( |
| 1087 | + "var", |
| 1088 | + [ |
| 1089 | + 2.72993945, |
| 1090 | + 1.58444294, |
| 1091 | + 4.14371708, |
| 1092 | + 4.92961687, |
| 1093 | + 2.7138744, |
| 1094 | + 3.48168586, |
| 1095 | + 0.69505519, |
| 1096 | + 1.87511994, |
| 1097 | + 4.20167276, |
| 1098 | + 0.04797675, |
| 1099 | + ], |
| 1100 | + 3, |
| 1101 | + 1, |
| 1102 | + "numpy_compute", |
| 1103 | + ), |
| 1104 | + ( |
| 1105 | + "std", |
| 1106 | + [ |
| 1107 | + 2.72993945, |
| 1108 | + 1.58444294, |
| 1109 | + 4.14371708, |
| 1110 | + 4.92961687, |
| 1111 | + 2.7138744, |
| 1112 | + 3.48168586, |
| 1113 | + 0.69505519, |
| 1114 | + 1.87511994, |
| 1115 | + 4.20167276, |
| 1116 | + 0.04797675, |
| 1117 | + ], |
| 1118 | + 3, |
| 1119 | + 1, |
| 1120 | + "numpy_compute", |
| 1121 | + ), |
| 1122 | + ( |
| 1123 | + "var", |
| 1124 | + [ |
| 1125 | + 2.72993945, |
| 1126 | + 1.58444294, |
| 1127 | + 4.14371708, |
| 1128 | + 4.92961687, |
| 1129 | + 2.7138744, |
| 1130 | + 3.48168586, |
| 1131 | + 0.69505519, |
| 1132 | + 1.87511994, |
| 1133 | + 4.20167276, |
| 1134 | + 0.04797675, |
| 1135 | + ], |
| 1136 | + 2, |
| 1137 | + 1, |
| 1138 | + "numpy_compute", |
| 1139 | + ), |
| 1140 | + ( |
| 1141 | + "std", |
| 1142 | + [ |
| 1143 | + 2.72993945, |
| 1144 | + 1.58444294, |
| 1145 | + 4.14371708, |
| 1146 | + 4.92961687, |
| 1147 | + 2.7138744, |
| 1148 | + 3.48168586, |
| 1149 | + 0.69505519, |
| 1150 | + 1.87511994, |
| 1151 | + 4.20167276, |
| 1152 | + 0.04797675, |
| 1153 | + ], |
| 1154 | + 2, |
| 1155 | + 1, |
| 1156 | + "numpy_compute", |
| 1157 | + ), |
| 1158 | + ("var", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0), |
| 1159 | + ("std", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0), |
| 1160 | + ("var", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0), |
| 1161 | + ("std", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0), |
| 1162 | + ("var", [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 5, 0, "numpy_compute"), |
| 1163 | + ], |
1100 | 1164 | )
|
1101 | 1165 | def test_rolling_var_correctness(func, values, window, ddof, exp_value):
|
1102 | 1166 | # This tests subsume the previous tests under test_rolling_var_numerical_issues
|
1103 | 1167 | # GH: 37051, 42064, 54333
|
1104 | 1168 | ts = Series(values)
|
1105 | 1169 | result = getattr(ts.rolling(window=window, center=True), func)(ddof=ddof)
|
1106 | 1170 | if result.last_valid_index():
|
1107 |
| - result = result[result.first_valid_index() : result.last_valid_index()+1].reset_index(drop=True) |
1108 |
| - expected = Series(getattr(sliding_window_view(values, window_shape=window), func)(axis=-1, ddof=ddof)) #.var(axis=-1, ddof=ddof)) |
| 1171 | + result = result[ |
| 1172 | + result.first_valid_index() : result.last_valid_index() + 1 |
| 1173 | + ].reset_index(drop=True) |
| 1174 | + expected = Series( |
| 1175 | + getattr(sliding_window_view(values, window_shape=window), func)( |
| 1176 | + axis=-1, ddof=ddof |
| 1177 | + ) |
| 1178 | + ) |
1109 | 1179 | tm.assert_series_equal(result, expected, atol=1e-55)
|
1110 | 1180 | # GH 42064
|
1111 | 1181 | if exp_value == 0:
|
1112 |
| - # new `roll_var` will output 0.0 correctly |
1113 |
| - tm.assert_series_equal(result==0, expected==0) |
| 1182 | + # new `roll_var` will output 0.0 correctly |
| 1183 | + tm.assert_series_equal(result == 0, expected == 0) |
| 1184 | + |
1114 | 1185 |
|
1115 | 1186 | def test_rolling_var_numerical_stability():
|
1116 | 1187 | # GH 52407
|
1117 |
| - A = [0.00000000e+00, 0.00000000e+00, 3.16188252e-18, 2.95781651e-16, |
1118 |
| - 2.23153542e-51, 0.00000000e+00, 0.00000000e+00, 5.39943432e-48, |
1119 |
| - 1.38206260e-73, 0.00000000e+00] |
| 1188 | + A = [ |
| 1189 | + 0.00000000e00, |
| 1190 | + 0.00000000e00, |
| 1191 | + 3.16188252e-18, |
| 1192 | + 2.95781651e-16, |
| 1193 | + 2.23153542e-51, |
| 1194 | + 0.00000000e00, |
| 1195 | + 0.00000000e00, |
| 1196 | + 5.39943432e-48, |
| 1197 | + 1.38206260e-73, |
| 1198 | + 0.00000000e00, |
| 1199 | + ] |
1120 | 1200 | ts = Series(A)
|
1121 | 1201 |
|
1122 | 1202 | result = ts.rolling(window=3, center=True).var(ddof=1)
|
1123 |
| - result = result[result.first_valid_index() : result.last_valid_index()+1].reset_index(drop=True) |
1124 |
| - |
| 1203 | + result = result[ |
| 1204 | + result.first_valid_index() : result.last_valid_index() + 1 |
| 1205 | + ].reset_index(drop=True) |
| 1206 | + |
1125 | 1207 | # numpy implementation
|
1126 | 1208 | expected = Series(sliding_window_view(A, window_shape=3).var(axis=-1, ddof=1))
|
1127 | 1209 | tm.assert_series_equal(result, expected, atol=1e-55)
|
1128 | 1210 |
|
| 1211 | + |
1129 | 1212 | def test_timeoffset_as_window_parameter_for_corr(unit):
|
1130 | 1213 | # GH: 28266
|
1131 | 1214 | dti = DatetimeIndex(
|
|
0 commit comments