Skip to content

TST/REF: Deprivate check_pairwise_moment and remove ConsistencyBase #34119

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 15 commits into from
May 11, 2020
Merged
24 changes: 10 additions & 14 deletions pandas/tests/window/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ def _create_data(self):
self.frame = DataFrame(randn(N, K), index=self.rng, columns=np.arange(K))


class ConsistencyBase(Base):
def _create_data(self):
super()._create_data()

def _check_pairwise_moment(self, dispatch, name, **kwargs):
def get_result(obj, obj2=None):
return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2)

result = get_result(self.frame)
result = result.loc[(slice(None), 1), 5]
result.index = result.index.droplevel(1)
expected = get_result(self.frame[1], self.frame[5])
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected, check_names=False)
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):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/window/moments/test_moments_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import pandas._testing as tm
from pandas.tests.window.common import (
Base,
ConsistencyBase,
check_binary_ew,
check_binary_ew_min_periods,
check_pairwise_moment,
ew_func,
moments_consistency_cov_data,
moments_consistency_is_constant,
Expand Down Expand Up @@ -274,15 +274,15 @@ def test_ew_min_periods(self, min_periods, name):
assert result2.dtype == np.float_


class TestEwmMomentsConsistency(ConsistencyBase):
class TestEwmMomentsConsistency(Base):
def setup_method(self, method):
self._create_data()

def test_ewmcov_pairwise(self):
self._check_pairwise_moment("ewm", "cov", span=10, min_periods=5)
check_pairwise_moment(self.frame, "ewm", "cov", span=10, min_periods=5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we turn these two test into 1 using pytest.mark.parameterize(func, ['cov', 'corr'])? Think there's similar potential below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, good catch!
parametrized! also for that two tests in rolling


def test_ewmcorr_pairwise(self):
self._check_pairwise_moment("ewm", "corr", span=10, min_periods=5)
check_pairwise_moment(self.frame, "ewm", "corr", span=10, min_periods=5)


@pytest.mark.parametrize("name", ["cov", "corr"])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/window/moments/test_moments_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas import DataFrame, Index, MultiIndex, Series, isna, notna
import pandas._testing as tm
from pandas.tests.window.common import (
ConsistencyBase,
Base,
moments_consistency_cov_data,
moments_consistency_is_constant,
moments_consistency_mock_mean,
Expand All @@ -18,7 +18,7 @@
)


class TestExpandingMomentsConsistency(ConsistencyBase):
class TestExpandingMomentsConsistency(Base):
def setup_method(self, method):
self._create_data()

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/window/moments/test_moments_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pandas.core.window.common import _flex_binary_moment
from pandas.tests.window.common import (
Base,
ConsistencyBase,
check_pairwise_moment,
moments_consistency_cov_data,
moments_consistency_is_constant,
moments_consistency_mock_mean,
Expand Down Expand Up @@ -942,7 +942,7 @@ def _rolling_consistency_cases():
yield window, min_periods, center


class TestRollingMomentsConsistency(ConsistencyBase):
class TestRollingMomentsConsistency(Base):
def setup_method(self, method):
self._create_data()

Expand All @@ -955,7 +955,7 @@ def test_rolling_cov(self):
tm.assert_almost_equal(result[-1], np.cov(A[-50:], B[-50:])[0, 1])

def test_rolling_cov_pairwise(self):
self._check_pairwise_moment("rolling", "cov", window=10, min_periods=5)
check_pairwise_moment(self.frame, "rolling", "cov", window=10, min_periods=5)

def test_rolling_corr(self):
A = self.series
Expand All @@ -974,7 +974,7 @@ def test_rolling_corr(self):
tm.assert_almost_equal(result[-1], a.corr(b))

def test_rolling_corr_pairwise(self):
self._check_pairwise_moment("rolling", "corr", window=10, min_periods=5)
check_pairwise_moment(self.frame, "rolling", "corr", window=10, min_periods=5)

@pytest.mark.parametrize("method", ["corr", "cov"])
def test_flex_binary_frame(self, method):
Expand Down