Skip to content

CLN: test_moments_consistency_*.py #36810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions pandas/tests/window/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +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)


def check_binary_ew(name, A, B):

result = ew_func(A=A, B=B, com=20, name=name, min_periods=5)
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)
# 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 = ew_func(empty, empty, 50, name=name, min_periods=min_periods)
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
)
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
Expand Down
43 changes: 34 additions & 9 deletions pandas/tests/window/moments/test_moments_consistency_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
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,
moments_consistency_mock_mean,
Expand All @@ -20,15 +17,43 @@

@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"])
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"])
Expand All @@ -38,7 +63,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down