Repository: pandas-dev/pandas
File: pandas/tests/window/test_rolling.py (Python)

Code before:
                1.87511994,
                4.20167276,
                0.04797675,
            ],
            2,
            1,
            "numpy_compute",
        ),
        ("var", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0),
        ("std", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0),
        ("var", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0),
        ("std", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0),
        ("var", [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 5, 0, "numpy_compute"),
    ],
)
def test_rolling_var_correctness(func, values, window, ddof, exp_value):
    # This tests subsume the previous tests under test_rolling_var_numerical_issues
    # GH: 37051, 42064, 54333
    ts = Series(values)
    result = getattr(ts.rolling(window=window, center=True), func)(ddof=ddof)
    if result.last_valid_index():
        result = result[
            result.first_valid_index() : result.last_valid_index() + 1
        ].reset_index(drop=True)
    expected = Series(
        getattr(sliding_window_view(values, window_shape=window), func)(
            axis=-1, ddof=ddof
        )
    )
    tm.assert_series_equal(result, expected, atol=1e-55)
    # GH 42064
    if exp_value == 0:
        # new `roll_var` will output 0.0 correctly
        tm.assert_series_equal(result == 0, expected == 0)


def test_rolling_var_numerical_stability():
    # GH 52407
    A = [
        0.00000000e00,
        0.00000000e00,
        3.16188252e-18,
        2.95781651e-16,
        2.23153542e-51,
        0.00000000e00,
        0.00000000e00,
        5.39943432e-48,
        1.38206260e-73,
        0.00000000e00,
    ]
    ts = Series(A)

Review comment (suggestion):
Is it possible to write out the expected values like the test did originally, rather than relying on another function to compute them? Its a bit easier to read when the expectations are explicitly stated, and it also avoids any confusion should issues with `sliding_window_view` later be discovered

Code after:
                1.75119007e00,
                1.64316767e00,
            ],
        ),
    ],
)
def test_rolling_var_correctness(func, values, window, ddof, expected_values):
    # GH: 37051, 42064, 54518, 52407, 47721
    ts = Series(values)
    result = getattr(ts.rolling(window=window), func)(ddof=ddof)
    if result.last_valid_index():
        result = result[
            result.first_valid_index() : result.last_valid_index() + 1
        ].reset_index(drop=True)
    expected = Series(expected_values)
    tm.assert_series_equal(result, expected, atol=1e-55)
    # GH 42064
    tm.assert_series_equal(result == 0, expected == 0)


def test_timeoffset_as_window_parameter_for_corr(unit):
    # GH: 28266
    dti = DatetimeIndex(
        [
            Timestamp("20130101 09:00:00"),
            Timestamp("20130102 09:00:02"),
            Timestamp("20130103 09:00:03"),
            Timestamp("20130105 09:00:05"),
            Timestamp("20130106 09:00:06"),
        ]
    ).as_unit(unit)
    mi = MultiIndex.from_product([dti, ["B", "A"]])

    exp = DataFrame(
        {
            "B": [
                np.nan,
                np.nan,
                0.9999999999999998,
                -1.0,
                1.0,
                -0.3273268353539892,
                0.9999999999999998,
                1.0,
                0.9999999999999998,
                1.0,
            ],
            "A": [
                np.nan,
                np.nan,
                -1.0,