-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST: split tests for windows to sub-modules #19667
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
Changes from all commits
bf59983
2a49a9a
a6a5e96
f235163
55b2661
1785d61
b9c121d
018685b
3cef534
9cb7a4f
9a4469e
52db43a
375c68d
8414a1f
229c9c1
2962a8f
4544afa
ccf473f
95d9d0e
e65af3b
a3abbb9
af463bb
6f74164
5ca693a
ac0f751
a8c0b01
9b99bf8
5fb9885
9778aa9
39e9292
02de0d7
05b00be
7343a88
8ee74d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from datetime import datetime | ||
|
||
import numpy as np | ||
import pytest | ||
from numpy.random import randn | ||
|
||
import pandas.core.window as rwindow | ||
import pandas.util.testing as tm | ||
from pandas import DataFrame, Series, bdate_range | ||
from pandas.errors import UnsupportedFunctionCall | ||
|
||
N, K = 100, 10 | ||
|
||
|
||
def assert_equal(left, right): | ||
if isinstance(left, Series): | ||
tm.assert_series_equal(left, right) | ||
else: | ||
tm.assert_frame_equal(left, right) | ||
|
||
|
||
class Base(object): | ||
|
||
_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)) | ||
|
||
|
||
class TestEWM(Base): | ||
|
||
def setup_method(self, method): | ||
self._create_data() | ||
|
||
def test_doc_string(self): | ||
|
||
df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) | ||
df | ||
df.ewm(com=0.5).mean() | ||
|
||
def test_constructor(self): | ||
for o in [self.series, self.frame]: | ||
c = o.ewm | ||
|
||
# valid | ||
c(com=0.5) | ||
c(span=1.5) | ||
c(alpha=0.5) | ||
c(halflife=0.75) | ||
c(com=0.5, span=None) | ||
c(alpha=0.5, com=None) | ||
c(halflife=0.75, alpha=None) | ||
|
||
# not valid: mutually exclusive | ||
with pytest.raises(ValueError): | ||
c(com=0.5, alpha=0.5) | ||
with pytest.raises(ValueError): | ||
c(span=1.5, halflife=0.75) | ||
with pytest.raises(ValueError): | ||
c(alpha=0.5, span=1.5) | ||
|
||
# not valid: com < 0 | ||
with pytest.raises(ValueError): | ||
c(com=-0.5) | ||
|
||
# not valid: span < 1 | ||
with pytest.raises(ValueError): | ||
c(span=0.5) | ||
|
||
# not valid: halflife <= 0 | ||
with pytest.raises(ValueError): | ||
c(halflife=0) | ||
|
||
# not valid: alpha <= 0 or alpha > 1 | ||
for alpha in (-0.5, 1.5): | ||
with pytest.raises(ValueError): | ||
c(alpha=alpha) | ||
|
||
def test_numpy_compat(self): | ||
# see gh-12811 | ||
e = rwindow.EWM(Series([2, 4, 6]), alpha=0.5) | ||
|
||
msg = "numpy operations are not valid with window objects" | ||
|
||
for func in ('std', 'mean', 'var'): | ||
tm.assert_raises_regex(UnsupportedFunctionCall, msg, | ||
getattr(e, func), 1, 2, 3) | ||
tm.assert_raises_regex(UnsupportedFunctionCall, msg, | ||
getattr(e, func), dtype=np.float64) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from datetime import datetime | ||
|
||
import numpy as np | ||
import pytest | ||
from numpy.random import randn | ||
|
||
import pandas as pd | ||
import pandas.core.window as rwindow | ||
import pandas.util.testing as tm | ||
from pandas import DataFrame, Series, bdate_range | ||
from pandas.errors import UnsupportedFunctionCall | ||
|
||
N, K = 100, 10 | ||
|
||
|
||
def assert_equal(left, right): | ||
if isinstance(left, Series): | ||
tm.assert_series_equal(left, right) | ||
else: | ||
tm.assert_frame_equal(left, right) | ||
|
||
|
||
class Base(object): | ||
|
||
_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)) | ||
|
||
|
||
class TestExpanding(Base): | ||
|
||
def setup_method(self, method): | ||
self._create_data() | ||
|
||
def test_doc_string(self): | ||
|
||
df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) | ||
df | ||
df.expanding(2).sum() | ||
|
||
def test_constructor(self): | ||
# GH 12669 | ||
|
||
for o in [self.series, self.frame]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a fixture for series and frame (in conftest.py) |
||
c = o.expanding | ||
|
||
# valid | ||
c(min_periods=1) | ||
c(min_periods=1, center=True) | ||
c(min_periods=1, center=False) | ||
|
||
# not valid | ||
for w in [2., 'foo', np.array([2])]: | ||
with pytest.raises(ValueError): | ||
c(min_periods=w) | ||
with pytest.raises(ValueError): | ||
c(min_periods=1, center=w) | ||
|
||
def test_numpy_compat(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parameterize this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you hlep me? Here I need use a decorator |
||
# see gh-12811 | ||
e = rwindow.Expanding(Series([2, 4, 6]), window=2) | ||
|
||
msg = "numpy operations are not valid with window objects" | ||
|
||
for func in ('std', 'mean', 'sum', 'max', 'min', 'var'): | ||
tm.assert_raises_regex(UnsupportedFunctionCall, msg, | ||
getattr(e, func), 1, 2, 3) | ||
tm.assert_raises_regex(UnsupportedFunctionCall, msg, | ||
getattr(e, func), dtype=np.float64) | ||
|
||
@pytest.mark.parametrize( | ||
'expander', | ||
[1, pytest.param('ls', marks=pytest.mark.xfail( | ||
reason='GH 16425 expanding with ' | ||
'offset not supported'))]) | ||
def test_empty_df_expanding(self, expander): | ||
# GH 15819 Verifies that datetime and integer expanding windows can be | ||
# applied to empty DataFrames | ||
|
||
expected = DataFrame() | ||
result = DataFrame().expanding(expander).sum() | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# Verifies that datetime and integer expanding windows can be applied | ||
# to empty DataFrames with datetime index | ||
expected = DataFrame(index=pd.DatetimeIndex([])) | ||
result = DataFrame( | ||
index=pd.DatetimeIndex([])).expanding(expander).sum() | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_missing_minp_zero(self): | ||
# https://github.com/pandas-dev/pandas/pull/18921 | ||
# minp=0 | ||
x = pd.Series([np.nan]) | ||
result = x.expanding(min_periods=0).sum() | ||
expected = pd.Series([0.0]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# minp=1 | ||
result = x.expanding(min_periods=1).sum() | ||
expected = pd.Series([np.nan]) | ||
tm.assert_series_equal(result, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see below