Skip to content

CLN/TST: Remove Base Class and all subclasses and fixturize data #34179

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 13 commits into from
May 15, 2020
22 changes: 1 addition & 21 deletions pandas/tests/window/common.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
from datetime import datetime

import numpy as np
from numpy.random import randn

from pandas import DataFrame, Series, bdate_range
from pandas import Series
import pandas._testing as tm

N, K = 100, 10


class Base:

_nan_locs = np.arange(20, 40)
_inf_locs = np.array([])

def _create_data(self):
arr = randn(N)
arr[self._nan_locs] = np.NaN

self.arr = arr
self.rng = bdate_range(datetime(2009, 1, 1), periods=N)
self.series = Series(arr.copy(), index=self.rng)
self.frame = DataFrame(randn(N, K), index=self.rng, columns=np.arange(K))


def check_pairwise_moment(frame, dispatch, name, **kwargs):
def get_result(obj, obj2=None):
Expand Down
62 changes: 61 additions & 1 deletion pandas/tests/window/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from datetime import datetime

import numpy as np
from numpy.random import randn
import pytest

import pandas.util._test_decorators as td

from pandas import DataFrame, Series, notna
from pandas import DataFrame, Series, bdate_range, notna


@pytest.fixture(params=[True, False])
Expand Down Expand Up @@ -242,3 +245,60 @@ def no_nans(x):
def consistency_data(request):
"""Create consistency data"""
return request.param


def _create_arr():
"""Internal function to mock an array."""
arr = randn(100)
locs = np.arange(20, 40)
arr[locs] = np.NaN
return arr


def _create_rng():
"""Internal function to mock date range."""
rng = bdate_range(datetime(2009, 1, 1), periods=100)
return rng


def _create_series():
"""Internal function to mock Series."""
arr = _create_arr()
series = Series(arr.copy(), index=_create_rng())
return series


def _create_frame():
"""Internal function to mock DataFrame."""
rng = _create_rng()
return DataFrame(randn(100, 10), index=rng, columns=np.arange(10))


@pytest.fixture
def nan_locs():
"""Make a range as loc fixture."""
return np.arange(20, 40)


@pytest.fixture
def arr():
"""Make an array as fixture."""
return _create_arr()


@pytest.fixture
def frame():
"""Make mocked frame as fixture."""
return _create_frame()


@pytest.fixture
def series():
"""Make mocked series as fixture."""
return _create_series()


@pytest.fixture(params=[_create_series(), _create_frame()])
def which(request):
"""Turn parametrized which as fixture for series and frame"""
return request.param
11 changes: 3 additions & 8 deletions pandas/tests/window/moments/test_moments_consistency_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pandas import DataFrame, Series, concat
from pandas.tests.window.common import (
Base,
check_binary_ew,
check_binary_ew_min_periods,
check_pairwise_moment,
Expand All @@ -19,13 +18,9 @@
)


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

@pytest.mark.parametrize("func", ["cov", "corr"])
def test_ewm_pairwise_cov_corr(self, func):
check_pairwise_moment(self.frame, "ewm", func, span=10, min_periods=5)
@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)


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


class TestExpandingMomentsConsistency(Base):
def setup_method(self, method):
self._create_data()
def _check_expanding(
func, static_comp, preserve_nan=True, series=None, frame=None, nan_locs=None
):

def test_expanding_corr(self):
A = self.series.dropna()
B = (A + randn(len(A)))[:-5]
series_result = func(series)
assert isinstance(series_result, Series)
frame_result = func(frame)
assert isinstance(frame_result, DataFrame)

result = A.expanding().corr(B)
result = func(series)
tm.assert_almost_equal(result[10], static_comp(series[:11]))

rolling_result = A.rolling(window=len(A), min_periods=1).corr(B)
if preserve_nan:
assert result.iloc[nan_locs].isna().all()

tm.assert_almost_equal(rolling_result, result)

def test_expanding_count(self):
result = self.series.expanding(min_periods=0).count()
tm.assert_almost_equal(
result, self.series.rolling(window=len(self.series), min_periods=0).count()
)
def _check_expanding_has_min_periods(func, static_comp, has_min_periods):
ser = Series(randn(50))

def test_expanding_quantile(self):
result = self.series.expanding().quantile(0.5)
if has_min_periods:
result = func(ser, min_periods=30)
assert result[:29].isna().all()
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))

rolling_result = self.series.rolling(
window=len(self.series), min_periods=1
).quantile(0.5)
# min_periods is working correctly
result = func(ser, min_periods=15)
assert isna(result.iloc[13])
assert notna(result.iloc[14])

tm.assert_almost_equal(result, rolling_result)
ser2 = Series(randn(20))
result = func(ser2, min_periods=5)
assert isna(result[3])
assert notna(result[4])

def test_expanding_cov(self):
A = self.series
B = (A + randn(len(A)))[:-5]
# min_periods=0
result0 = func(ser, min_periods=0)
result1 = func(ser, min_periods=1)
tm.assert_almost_equal(result0, result1)
else:
result = func(ser)
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))

result = A.expanding().cov(B)

rolling_result = A.rolling(window=len(A), min_periods=1).cov(B)
def test_expanding_corr(series):
A = series.dropna()
B = (A + randn(len(A)))[:-5]

tm.assert_almost_equal(rolling_result, result)
result = A.expanding().corr(B)

def test_expanding_cov_pairwise(self):
result = self.frame.expanding().corr()
rolling_result = A.rolling(window=len(A), min_periods=1).corr(B)

rolling_result = self.frame.rolling(
window=len(self.frame), min_periods=1
).corr()
tm.assert_almost_equal(rolling_result, result)

tm.assert_frame_equal(result, rolling_result)

def test_expanding_corr_pairwise(self):
result = self.frame.expanding().corr()
def test_expanding_count(series):
result = series.expanding(min_periods=0).count()
tm.assert_almost_equal(
result, series.rolling(window=len(series), min_periods=0).count()
)

rolling_result = self.frame.rolling(
window=len(self.frame), min_periods=1
).corr()
tm.assert_frame_equal(result, rolling_result)

@pytest.mark.parametrize("has_min_periods", [True, False])
@pytest.mark.parametrize(
"func,static_comp",
[("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)],
ids=["sum", "mean", "max", "min"],
)
def test_expanding_func(self, func, static_comp, has_min_periods):
def expanding_func(x, min_periods=1, center=False, axis=0):
exp = x.expanding(min_periods=min_periods, center=center, axis=axis)
return getattr(exp, func)()

self._check_expanding(expanding_func, static_comp, preserve_nan=False)
self._check_expanding_has_min_periods(
expanding_func, static_comp, has_min_periods
)
def test_expanding_quantile(series):
result = series.expanding().quantile(0.5)

rolling_result = series.rolling(window=len(series), min_periods=1).quantile(0.5)

tm.assert_almost_equal(result, rolling_result)


@pytest.mark.parametrize("has_min_periods", [True, False])
def test_expanding_apply(self, engine_and_raw, has_min_periods):
def test_expanding_cov(series):
A = series
B = (A + randn(len(A)))[:-5]

engine, raw = engine_and_raw
result = A.expanding().cov(B)

def expanding_mean(x, min_periods=1):
rolling_result = A.rolling(window=len(A), min_periods=1).cov(B)

exp = x.expanding(min_periods=min_periods)
result = exp.apply(lambda x: x.mean(), raw=raw, engine=engine)
return result
tm.assert_almost_equal(rolling_result, result)

# TODO(jreback), needed to add preserve_nan=False
# here to make this pass
self._check_expanding(expanding_mean, np.mean, preserve_nan=False)
self._check_expanding_has_min_periods(expanding_mean, np.mean, has_min_periods)

def _check_expanding(self, func, static_comp, preserve_nan=True):
def test_expanding_cov_pairwise(frame):
result = frame.expanding().cov()

series_result = func(self.series)
assert isinstance(series_result, Series)
frame_result = func(self.frame)
assert isinstance(frame_result, DataFrame)
rolling_result = frame.rolling(window=len(frame), min_periods=1).cov()

result = func(self.series)
tm.assert_almost_equal(result[10], static_comp(self.series[:11]))
tm.assert_frame_equal(result, rolling_result)

if preserve_nan:
assert result.iloc[self._nan_locs].isna().all()

def _check_expanding_has_min_periods(self, func, static_comp, has_min_periods):
ser = Series(randn(50))
def test_expanding_corr_pairwise(frame):
result = frame.expanding().corr()

if has_min_periods:
result = func(ser, min_periods=30)
assert result[:29].isna().all()
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
rolling_result = frame.rolling(window=len(frame), min_periods=1).corr()
tm.assert_frame_equal(result, rolling_result)

# min_periods is working correctly
result = func(ser, min_periods=15)
assert isna(result.iloc[13])
assert notna(result.iloc[14])

ser2 = Series(randn(20))
result = func(ser2, min_periods=5)
assert isna(result[3])
assert notna(result[4])
@pytest.mark.parametrize("has_min_periods", [True, False])
@pytest.mark.parametrize(
"func,static_comp",
[("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)],
ids=["sum", "mean", "max", "min"],
)
def test_expanding_func(func, static_comp, has_min_periods, series, frame, nan_locs):
def expanding_func(x, min_periods=1, center=False, axis=0):
exp = x.expanding(min_periods=min_periods, center=center, axis=axis)
return getattr(exp, func)()

_check_expanding(
expanding_func,
static_comp,
preserve_nan=False,
series=series,
frame=frame,
nan_locs=nan_locs,
)
_check_expanding_has_min_periods(expanding_func, static_comp, has_min_periods)


# min_periods=0
result0 = func(ser, min_periods=0)
result1 = func(ser, min_periods=1)
tm.assert_almost_equal(result0, result1)
else:
result = func(ser)
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
@pytest.mark.parametrize("has_min_periods", [True, False])
def test_expanding_apply(engine_and_raw, has_min_periods, series, frame, nan_locs):

engine, raw = engine_and_raw

def expanding_mean(x, min_periods=1):

exp = x.expanding(min_periods=min_periods)
result = exp.apply(lambda x: x.mean(), raw=raw, engine=engine)
return result

# TODO(jreback), needed to add preserve_nan=False
# here to make this pass
_check_expanding(
expanding_mean,
np.mean,
preserve_nan=False,
series=series,
frame=frame,
nan_locs=nan_locs,
)
_check_expanding_has_min_periods(expanding_mean, np.mean, has_min_periods)


@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4])
Expand Down
Loading