Skip to content

CLN: More tests/window/* #37959

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
Nov 19, 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
32 changes: 8 additions & 24 deletions pandas/tests/window/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,40 +275,24 @@ def consistency_data(request):
return request.param


def _create_series():
"""Internal function to mock Series."""
arr = np.random.randn(100)
locs = np.arange(20, 40)
arr[locs] = np.NaN
series = Series(arr, index=bdate_range(datetime(2009, 1, 1), periods=100))
return series


def _create_frame():
"""Internal function to mock DataFrame."""
@pytest.fixture
def frame():
"""Make mocked frame as fixture."""
return DataFrame(
np.random.randn(100, 10),
index=bdate_range(datetime(2009, 1, 1), periods=100),
columns=np.arange(10),
)


@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
arr = np.random.randn(100)
locs = np.arange(20, 40)
arr[locs] = np.NaN
series = Series(arr, index=bdate_range(datetime(2009, 1, 1), periods=100))
return series


@pytest.fixture(params=["1 day", timedelta(days=1)])
Expand Down
26 changes: 0 additions & 26 deletions pandas/tests/window/test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

import pandas as pd
from pandas import DataFrame, Index, Series, Timestamp, concat
import pandas._testing as tm
Expand Down Expand Up @@ -238,30 +236,6 @@ def test_count_nonnumeric_types():
tm.assert_frame_equal(result, expected)


@td.skip_if_no_scipy
@pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
def test_window_with_args():
# make sure that we are aggregating window functions correctly with arg
r = Series(np.random.randn(100)).rolling(
window=10, min_periods=1, win_type="gaussian"
)
expected = concat([r.mean(std=10), r.mean(std=0.01)], axis=1)
expected.columns = ["<lambda>", "<lambda>"]
result = r.aggregate([lambda x: x.mean(std=10), lambda x: x.mean(std=0.01)])
tm.assert_frame_equal(result, expected)

def a(x):
return x.mean(std=10)

def b(x):
return x.mean(std=0.01)

expected = concat([r.mean(std=10), r.mean(std=0.01)], axis=1)
expected.columns = ["a", "b"]
result = r.aggregate([a, b])
tm.assert_frame_equal(result, expected)


def test_preserve_metadata():
# GH 10565
s = Series(np.arange(100), name="foo")
Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/window/test_apply.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import numpy as np
import pytest

from pandas.errors import NumbaUtilError
import pandas.util._test_decorators as td

from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range
import pandas._testing as tm

Expand Down Expand Up @@ -133,14 +130,6 @@ def test_invalid_raw_numba():
Series(range(1)).rolling(1).apply(lambda x: x, raw=False, engine="numba")


@td.skip_if_no("numba")
def test_invalid_kwargs_nopython():
with pytest.raises(NumbaUtilError, match="numba does not support kwargs with"):
Series(range(1)).rolling(1).apply(
lambda x: x, kwargs={"a": 1}, engine="numba", raw=True
)


@pytest.mark.parametrize("args_kwargs", [[None, {"par": 10}], [(10,), None]])
def test_rolling_apply_args_kwargs(args_kwargs):
# GH 33433
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/window/test_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def test_doc_string():
df.ewm(com=0.5).mean()


def test_constructor(which):
def test_constructor(frame_or_series):

c = which.ewm
c = frame_or_series(range(5)).ewm

# valid
c(com=0.5)
Expand Down
34 changes: 15 additions & 19 deletions pandas/tests/window/test_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def test_doc_string():
@pytest.mark.filterwarnings(
"ignore:The `center` argument on `expanding` will be removed in the future"
)
def test_constructor(which):
def test_constructor(frame_or_series):
# GH 12669

c = which.expanding
c = frame_or_series(range(5)).expanding

# valid
c(min_periods=1)
Expand All @@ -34,10 +34,10 @@ def test_constructor(which):
@pytest.mark.filterwarnings(
"ignore:The `center` argument on `expanding` will be removed in the future"
)
def test_constructor_invalid(which, w):
def test_constructor_invalid(frame_or_series, w):
# not valid

c = which.expanding
c = frame_or_series(range(5)).expanding
msg = "min_periods must be an integer"
with pytest.raises(ValueError, match=msg):
c(min_periods=w)
Expand Down Expand Up @@ -118,30 +118,27 @@ def test_expanding_axis(axis_frame):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("constructor", [Series, DataFrame])
def test_expanding_count_with_min_periods(constructor):
def test_expanding_count_with_min_periods(frame_or_series):
# GH 26996
result = constructor(range(5)).expanding(min_periods=3).count()
expected = constructor([np.nan, np.nan, 3.0, 4.0, 5.0])
result = frame_or_series(range(5)).expanding(min_periods=3).count()
expected = frame_or_series([np.nan, np.nan, 3.0, 4.0, 5.0])
tm.assert_equal(result, expected)


@pytest.mark.parametrize("constructor", [Series, DataFrame])
def test_expanding_count_default_min_periods_with_null_values(constructor):
def test_expanding_count_default_min_periods_with_null_values(frame_or_series):
# GH 26996
values = [1, 2, 3, np.nan, 4, 5, 6]
expected_counts = [1.0, 2.0, 3.0, 3.0, 4.0, 5.0, 6.0]

result = constructor(values).expanding().count()
expected = constructor(expected_counts)
result = frame_or_series(values).expanding().count()
expected = frame_or_series(expected_counts)
tm.assert_equal(result, expected)


@pytest.mark.parametrize("constructor", [Series, DataFrame])
def test_expanding_count_with_min_periods_exceeding_series_length(constructor):
def test_expanding_count_with_min_periods_exceeding_series_length(frame_or_series):
# GH 25857
result = constructor(range(5)).expanding(min_periods=6).count()
expected = constructor([np.nan, np.nan, np.nan, np.nan, np.nan])
result = frame_or_series(range(5)).expanding(min_periods=6).count()
expected = frame_or_series([np.nan, np.nan, np.nan, np.nan, np.nan])
tm.assert_equal(result, expected)


Expand Down Expand Up @@ -246,10 +243,9 @@ def test_center_deprecate_warning():
df.expanding()


@pytest.mark.parametrize("constructor", ["DataFrame", "Series"])
def test_expanding_sem(constructor):
def test_expanding_sem(frame_or_series):
# GH: 26476
obj = getattr(pd, constructor)([0, 1, 2])
obj = frame_or_series([0, 1, 2])
result = obj.expanding().sem()
if isinstance(result, DataFrame):
result = Series(result[0].values)
Expand Down
132 changes: 68 additions & 64 deletions pandas/tests/window/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from pandas.core.groupby.groupby import get_groupby


class TestGrouperGrouping:
class TestRolling:
def setup_method(self):
self.series = Series(np.arange(10))
self.frame = DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})

def test_mutated(self):
Expand Down Expand Up @@ -152,68 +151,6 @@ def test_rolling_apply_mutability(self):
result = g.rolling(window=2).sum()
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"f", ["sum", "mean", "min", "max", "count", "kurt", "skew"]
)
def test_expanding(self, f):
g = self.frame.groupby("A")
r = g.expanding()

result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.expanding(), f)())
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["std", "var"])
def test_expanding_ddof(self, f):
g = self.frame.groupby("A")
r = g.expanding()

result = getattr(r, f)(ddof=0)
expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0))
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"interpolation", ["linear", "lower", "higher", "midpoint", "nearest"]
)
def test_expanding_quantile(self, interpolation):
g = self.frame.groupby("A")
r = g.expanding()
result = r.quantile(0.4, interpolation=interpolation)
expected = g.apply(
lambda x: x.expanding().quantile(0.4, interpolation=interpolation)
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["corr", "cov"])
def test_expanding_corr_cov(self, f):
g = self.frame.groupby("A")
r = g.expanding()

result = getattr(r, f)(self.frame)

def func(x):
return getattr(x.expanding(), f)(self.frame)

expected = g.apply(func)
tm.assert_frame_equal(result, expected)

result = getattr(r.B, f)(pairwise=True)

def func(x):
return getattr(x.B.expanding(), f)(pairwise=True)

expected = g.apply(func)
tm.assert_series_equal(result, expected)

def test_expanding_apply(self, raw):
g = self.frame.groupby("A")
r = g.expanding()

# reduction
result = r.apply(lambda x: x.sum(), raw=raw)
expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw))
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("expected_value,raw_value", [[1.0, True], [0.0, False]])
def test_groupby_rolling(self, expected_value, raw_value):
# GH 31754
Expand Down Expand Up @@ -633,6 +570,73 @@ def test_groupby_rolling_index_level_and_column_label(self):
tm.assert_frame_equal(result, expected)


class TestExpanding:
def setup_method(self):
self.frame = DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})

@pytest.mark.parametrize(
"f", ["sum", "mean", "min", "max", "count", "kurt", "skew"]
)
def test_expanding(self, f):
g = self.frame.groupby("A")
r = g.expanding()

result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.expanding(), f)())
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["std", "var"])
def test_expanding_ddof(self, f):
g = self.frame.groupby("A")
r = g.expanding()

result = getattr(r, f)(ddof=0)
expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0))
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"interpolation", ["linear", "lower", "higher", "midpoint", "nearest"]
)
def test_expanding_quantile(self, interpolation):
g = self.frame.groupby("A")
r = g.expanding()
result = r.quantile(0.4, interpolation=interpolation)
expected = g.apply(
lambda x: x.expanding().quantile(0.4, interpolation=interpolation)
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("f", ["corr", "cov"])
def test_expanding_corr_cov(self, f):
g = self.frame.groupby("A")
r = g.expanding()

result = getattr(r, f)(self.frame)

def func(x):
return getattr(x.expanding(), f)(self.frame)

expected = g.apply(func)
tm.assert_frame_equal(result, expected)

result = getattr(r.B, f)(pairwise=True)

def func(x):
return getattr(x.B.expanding(), f)(pairwise=True)

expected = g.apply(func)
tm.assert_series_equal(result, expected)

def test_expanding_apply(self, raw):
g = self.frame.groupby("A")
r = g.expanding()

# reduction
result = r.apply(lambda x: x.sum(), raw=raw)
expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw))
tm.assert_frame_equal(result, expected)


class TestEWM:
@pytest.mark.parametrize(
"method, expected_data",
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/window/test_numba.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

from pandas.errors import NumbaUtilError
import pandas.util._test_decorators as td

from pandas import DataFrame, Series, option_context
Expand Down Expand Up @@ -112,3 +113,11 @@ def f(x):
result = s.rolling(2).apply(f, engine=None, raw=True)
expected = s.rolling(2).apply(f, engine="numba", raw=True)
tm.assert_series_equal(expected, result)


@td.skip_if_no("numba", "0.46.0")
def test_invalid_kwargs_nopython():
with pytest.raises(NumbaUtilError, match="numba does not support kwargs with"):
Series(range(1)).rolling(1).apply(
lambda x: x, kwargs={"a": 1}, engine="numba", raw=True
)
Loading