Skip to content

Commit d58ebdf

Browse files
committed
feat: consolidate tests for rolling variance correctness against numpy
1 parent 9960e95 commit d58ebdf

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

pandas/tests/window/test_rolling.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
)
55

66
import numpy as np
7+
from numpy.lib.stride_tricks import sliding_window_view
8+
9+
710
import pytest
811

912
from pandas.compat import (
@@ -1081,30 +1084,47 @@ def test_rolling_sem(frame_or_series):
10811084
expected = Series([np.nan] + [0.7071067811865476] * 2)
10821085
tm.assert_series_equal(result, expected)
10831086

1084-
1085-
@pytest.mark.xfail(
1086-
is_platform_arm() or is_platform_power() or is_platform_riscv64(),
1087-
reason="GH 38921",
1088-
)
10891087
@pytest.mark.parametrize(
1090-
("func", "third_value", "values"),
1091-
[
1092-
("var", 1, [5e33, 0, 0.5, 0.5, 2, 0]),
1093-
("std", 1, [7.071068e16, 0, 0.7071068, 0.7071068, 1.414214, 0]),
1094-
("var", 2, [5e33, 0.5, 0, 0.5, 2, 0]),
1095-
("std", 2, [7.071068e16, 0.7071068, 0, 0.7071068, 1.414214, 0]),
1096-
],
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+
],
10971100
)
1098-
def test_rolling_var_numerical_issues(func, third_value, values):
1099-
# GH: 37051
1100-
ds = Series([99999999999999999, 1, third_value, 2, 3, 1, 1])
1101-
result = getattr(ds.rolling(2), func)()
1102-
expected = Series([np.nan] + values)
1103-
tm.assert_series_equal(result, expected)
1101+
def test_rolling_var_correctness(func, values, window, ddof, exp_value):
1102+
# This tests subsume the previous tests under test_rolling_var_numerical_issues
1103+
# GH: 37051, 42064, 54333
1104+
ts = Series(values)
1105+
result = getattr(ts.rolling(window=window, center=True), func)(ddof=ddof)
1106+
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))
1109+
tm.assert_series_equal(result, expected, atol=1e-55)
11041110
# GH 42064
1105-
# new `roll_var` will output 0.0 correctly
1106-
tm.assert_series_equal(result == 0, expected == 0)
1107-
1111+
if exp_value == 0:
1112+
# new `roll_var` will output 0.0 correctly
1113+
tm.assert_series_equal(result==0, expected==0)
1114+
1115+
def test_rolling_var_numerical_stability():
1116+
# 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]
1120+
ts = Series(A)
1121+
1122+
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+
1125+
# numpy implementation
1126+
expected = Series(sliding_window_view(A, window_shape=3).var(axis=-1, ddof=1))
1127+
tm.assert_series_equal(result, expected, atol=1e-55)
11081128

11091129
def test_timeoffset_as_window_parameter_for_corr(unit):
11101130
# GH: 28266

0 commit comments

Comments
 (0)