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

Code before:
            "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",
        ),
        ("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,

Review comment (refactor):
Super nit but you can remove this first comment - if the user cared to see the history they can consult the git blame

Code after:
                1.41421356e00,
                1.87082869e00,
                4.08248290e09,
                4.08248290e09,
                4.08248290e09,
                4.08248290e09,
                4.08248290e09,
                4.08248290e09,
                1.72240142e00,
                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,