From 53962841974dec5a3fc6d08b8e0164cc579f8869 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Thu, 1 Oct 2020 20:18:33 -0700 Subject: [PATCH 1/3] Remove check_pairwise --- pandas/tests/window/common.py | 12 ------------ .../window/moments/test_moments_consistency_ewm.py | 9 +++++++-- .../moments/test_moments_consistency_rolling.py | 8 ++++++-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index 7e0be331ec8d5..8c4925db3047d 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -4,18 +4,6 @@ import pandas._testing as tm -def check_pairwise_moment(frame, dispatch, name, **kwargs): - def get_result(obj, obj2=None): - return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2) - - result = get_result(frame) - result = result.loc[(slice(None), 1), 5] - result.index = result.index.droplevel(1) - expected = get_result(frame[1], frame[5]) - expected.index = expected.index._with_freq(None) - tm.assert_series_equal(result, expected, check_names=False) - - def ew_func(A, B, com, name, **kwargs): return getattr(A.ewm(com, **kwargs), name)(B) diff --git a/pandas/tests/window/moments/test_moments_consistency_ewm.py b/pandas/tests/window/moments/test_moments_consistency_ewm.py index f143278e12ec5..239b9c4156b9f 100644 --- a/pandas/tests/window/moments/test_moments_consistency_ewm.py +++ b/pandas/tests/window/moments/test_moments_consistency_ewm.py @@ -3,10 +3,10 @@ import pytest from pandas import DataFrame, Series, concat +import pandas._testing as tm from pandas.tests.window.common import ( check_binary_ew, check_binary_ew_min_periods, - check_pairwise_moment, ew_func, moments_consistency_cov_data, moments_consistency_is_constant, @@ -20,7 +20,12 @@ @pytest.mark.parametrize("func", ["cov", "corr"]) def test_ewm_pairwise_cov_corr(func, frame): - check_pairwise_moment(frame, "ewm", func, span=10, min_periods=5) + result = getattr(frame.ewm(span=10, min_periods=5), func)() + result = result.loc[(slice(None), 1), 5] + result.index = result.index.droplevel(1) + expected = getattr(frame[1].ewm(span=10, min_periods=5), func)(frame[5]) + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(result, expected, check_names=False) @pytest.mark.parametrize("name", ["cov", "corr"]) diff --git a/pandas/tests/window/moments/test_moments_consistency_rolling.py b/pandas/tests/window/moments/test_moments_consistency_rolling.py index dfcbdde466d44..f9670e0c30ade 100644 --- a/pandas/tests/window/moments/test_moments_consistency_rolling.py +++ b/pandas/tests/window/moments/test_moments_consistency_rolling.py @@ -12,7 +12,6 @@ import pandas._testing as tm from pandas.core.window.common import flex_binary_moment from pandas.tests.window.common import ( - check_pairwise_moment, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -60,7 +59,12 @@ def test_rolling_corr(series): @pytest.mark.parametrize("func", ["cov", "corr"]) def test_rolling_pairwise_cov_corr(func, frame): - check_pairwise_moment(frame, "rolling", func, window=10, min_periods=5) + result = getattr(frame.rolling(window=10, min_periods=5), func)() + result = result.loc[(slice(None), 1), 5] + result.index = result.index.droplevel(1) + expected = getattr(frame[1].rolling(window=10, min_periods=5), func)(frame[5]) + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(result, expected, check_names=False) @pytest.mark.parametrize("method", ["corr", "cov"]) From 35a57482fa81385280aa6245a3ffa4d742a13ea5 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 2 Oct 2020 10:18:32 -0700 Subject: [PATCH 2/3] Remove ew_func --- pandas/tests/window/common.py | 14 +++++--------- .../window/moments/test_moments_consistency_ewm.py | 3 +-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index 8c4925db3047d..afe72dd934d14 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -4,20 +4,16 @@ import pandas._testing as tm -def ew_func(A, B, com, name, **kwargs): - return getattr(A.ewm(com, **kwargs), name)(B) - - def check_binary_ew(name, A, B): - result = ew_func(A=A, B=B, com=20, name=name, min_periods=5) + result = getattr(A.ewm(com=20, min_periods=5), name)(B) assert np.isnan(result.values[:14]).all() assert not np.isnan(result.values[14:]).any() def check_binary_ew_min_periods(name, min_periods, A, B): # GH 7898 - result = ew_func(A, B, 20, name=name, min_periods=min_periods) + result = getattr(A.ewm(com=20, min_periods=min_periods), name)(B) # binary functions (ewmcov, ewmcorr) with bias=False require at # least two values assert np.isnan(result.values[:11]).all() @@ -25,12 +21,12 @@ def check_binary_ew_min_periods(name, min_periods, A, B): # check series of length 0 empty = Series([], dtype=np.float64) - result = ew_func(empty, empty, 50, name=name, min_periods=min_periods) + result = getattr(empty.ewm(com=50, min_periods=min_periods), name)(empty) tm.assert_series_equal(result, empty) # check series of length 1 - result = ew_func( - Series([1.0]), Series([1.0]), 50, name=name, min_periods=min_periods + result = getattr(Series([1.0]).ewm(com=50, min_periods=min_periods), name)( + Series([1.0]) ) tm.assert_series_equal(result, Series([np.NaN])) diff --git a/pandas/tests/window/moments/test_moments_consistency_ewm.py b/pandas/tests/window/moments/test_moments_consistency_ewm.py index 239b9c4156b9f..582b2d3cd1312 100644 --- a/pandas/tests/window/moments/test_moments_consistency_ewm.py +++ b/pandas/tests/window/moments/test_moments_consistency_ewm.py @@ -7,7 +7,6 @@ from pandas.tests.window.common import ( check_binary_ew, check_binary_ew_min_periods, - ew_func, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -43,7 +42,7 @@ def test_different_input_array_raise_exception(name, binary_ew_data): msg = "Input arrays must be of the same type!" # exception raised is Exception with pytest.raises(Exception, match=msg): - ew_func(A, randn(50), 20, name=name, min_periods=5) + getattr(A.ewm(com=20, min_periods=5), name)(randn(50)) @pytest.mark.slow From a704a7531954bfdb58aac03755500adb227de2ea Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 2 Oct 2020 10:27:12 -0700 Subject: [PATCH 3/3] Remove check_binary_ew and check_binary_ew_min_periods --- pandas/tests/window/common.py | 27 ---------------- .../moments/test_moments_consistency_ewm.py | 31 ++++++++++++++++--- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index afe72dd934d14..7c8c9de40f7c5 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -4,33 +4,6 @@ import pandas._testing as tm -def check_binary_ew(name, A, B): - - result = getattr(A.ewm(com=20, min_periods=5), name)(B) - assert np.isnan(result.values[:14]).all() - assert not np.isnan(result.values[14:]).any() - - -def check_binary_ew_min_periods(name, min_periods, A, B): - # GH 7898 - result = getattr(A.ewm(com=20, min_periods=min_periods), name)(B) - # binary functions (ewmcov, ewmcorr) with bias=False require at - # least two values - assert np.isnan(result.values[:11]).all() - assert not np.isnan(result.values[11:]).any() - - # check series of length 0 - empty = Series([], dtype=np.float64) - result = getattr(empty.ewm(com=50, min_periods=min_periods), name)(empty) - tm.assert_series_equal(result, empty) - - # check series of length 1 - result = getattr(Series([1.0]).ewm(com=50, min_periods=min_periods), name)( - Series([1.0]) - ) - tm.assert_series_equal(result, Series([np.NaN])) - - def moments_consistency_mock_mean(x, mean, mock_mean): mean_x = mean(x) # check that correlation of a series with itself is either 1 or NaN diff --git a/pandas/tests/window/moments/test_moments_consistency_ewm.py b/pandas/tests/window/moments/test_moments_consistency_ewm.py index 582b2d3cd1312..089ec697b5b1c 100644 --- a/pandas/tests/window/moments/test_moments_consistency_ewm.py +++ b/pandas/tests/window/moments/test_moments_consistency_ewm.py @@ -5,8 +5,6 @@ from pandas import DataFrame, Series, concat import pandas._testing as tm from pandas.tests.window.common import ( - check_binary_ew, - check_binary_ew_min_periods, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -28,11 +26,34 @@ def test_ewm_pairwise_cov_corr(func, frame): @pytest.mark.parametrize("name", ["cov", "corr"]) -def test_ewm_corr_cov(name, min_periods, binary_ew_data): +def test_ewm_corr_cov(name, binary_ew_data): A, B = binary_ew_data - check_binary_ew(name="corr", A=A, B=B) - check_binary_ew_min_periods("corr", min_periods, A, B) + result = getattr(A.ewm(com=20, min_periods=5), name)(B) + assert np.isnan(result.values[:14]).all() + assert not np.isnan(result.values[14:]).any() + + +@pytest.mark.parametrize("name", ["cov", "corr"]) +def test_ewm_corr_cov_min_periods(name, min_periods, binary_ew_data): + # GH 7898 + A, B = binary_ew_data + result = getattr(A.ewm(com=20, min_periods=min_periods), name)(B) + # binary functions (ewmcov, ewmcorr) with bias=False require at + # least two values + assert np.isnan(result.values[:11]).all() + assert not np.isnan(result.values[11:]).any() + + # check series of length 0 + empty = Series([], dtype=np.float64) + result = getattr(empty.ewm(com=50, min_periods=min_periods), name)(empty) + tm.assert_series_equal(result, empty) + + # check series of length 1 + result = getattr(Series([1.0]).ewm(com=50, min_periods=min_periods), name)( + Series([1.0]) + ) + tm.assert_series_equal(result, Series([np.NaN])) @pytest.mark.parametrize("name", ["cov", "corr"])