Skip to content

Commit 375c68d

Browse files
committed
Merge branch 'TST--split-tests-for-windows-to-sub-modules-pandas-dev#19228'
2 parents 9a4469e + 52db43a commit 375c68d

File tree

5 files changed

+1033
-282
lines changed

5 files changed

+1033
-282
lines changed

pandas/tests/windows/__init__.py

Whitespace-only changes.

pandas/tests/windows/test_ewma.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from datetime import datetime
2+
3+
import numpy as np
4+
import pytest
5+
from numpy.random import randn
6+
7+
import pandas.core.window as rwindow
8+
import pandas.util.testing as tm
9+
from pandas import DataFrame, Series, bdate_range
10+
from pandas.errors import UnsupportedFunctionCall
11+
12+
N, K = 100, 10
13+
14+
15+
def assert_equal(left, right):
16+
if isinstance(left, Series):
17+
tm.assert_series_equal(left, right)
18+
else:
19+
tm.assert_frame_equal(left, right)
20+
21+
22+
class Base(object):
23+
24+
_nan_locs = np.arange(20, 40)
25+
_inf_locs = np.array([])
26+
27+
def _create_data(self):
28+
arr = randn(N)
29+
arr[self._nan_locs] = np.NaN
30+
31+
self.arr = arr
32+
self.rng = bdate_range(datetime(2009, 1, 1), periods=N)
33+
self.series = Series(arr.copy(), index=self.rng)
34+
self.frame = DataFrame(randn(N, K), index=self.rng,
35+
columns=np.arange(K))
36+
37+
38+
class TestEWM(Base):
39+
40+
def setup_method(self, method):
41+
self._create_data()
42+
43+
def test_doc_string(self):
44+
45+
df = DataFrame({'B': [0, 1, 2, np.nan, 4]})
46+
df
47+
df.ewm(com=0.5).mean()
48+
49+
def test_constructor(self):
50+
for o in [self.series, self.frame]:
51+
c = o.ewm
52+
53+
# valid
54+
c(com=0.5)
55+
c(span=1.5)
56+
c(alpha=0.5)
57+
c(halflife=0.75)
58+
c(com=0.5, span=None)
59+
c(alpha=0.5, com=None)
60+
c(halflife=0.75, alpha=None)
61+
62+
# not valid: mutually exclusive
63+
with pytest.raises(ValueError):
64+
c(com=0.5, alpha=0.5)
65+
with pytest.raises(ValueError):
66+
c(span=1.5, halflife=0.75)
67+
with pytest.raises(ValueError):
68+
c(alpha=0.5, span=1.5)
69+
70+
# not valid: com < 0
71+
with pytest.raises(ValueError):
72+
c(com=-0.5)
73+
74+
# not valid: span < 1
75+
with pytest.raises(ValueError):
76+
c(span=0.5)
77+
78+
# not valid: halflife <= 0
79+
with pytest.raises(ValueError):
80+
c(halflife=0)
81+
82+
# not valid: alpha <= 0 or alpha > 1
83+
for alpha in (-0.5, 1.5):
84+
with pytest.raises(ValueError):
85+
c(alpha=alpha)
86+
87+
def test_numpy_compat(self):
88+
# see gh-12811
89+
e = rwindow.EWM(Series([2, 4, 6]), alpha=0.5)
90+
91+
msg = "numpy operations are not valid with window objects"
92+
93+
for func in ('std', 'mean', 'var'):
94+
tm.assert_raises_regex(UnsupportedFunctionCall, msg,
95+
getattr(e, func), 1, 2, 3)
96+
tm.assert_raises_regex(UnsupportedFunctionCall, msg,
97+
getattr(e, func), dtype=np.float64)
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from datetime import datetime
2+
3+
import numpy as np
4+
import pytest
5+
from numpy.random import randn
6+
7+
import pandas as pd
8+
import pandas.core.window as rwindow
9+
import pandas.util.testing as tm
10+
from pandas import DataFrame, Series, bdate_range
11+
from pandas.errors import UnsupportedFunctionCall
12+
13+
N, K = 100, 10
14+
15+
16+
def assert_equal(left, right):
17+
if isinstance(left, Series):
18+
tm.assert_series_equal(left, right)
19+
else:
20+
tm.assert_frame_equal(left, right)
21+
22+
23+
class Base(object):
24+
25+
_nan_locs = np.arange(20, 40)
26+
_inf_locs = np.array([])
27+
28+
def _create_data(self):
29+
arr = randn(N)
30+
arr[self._nan_locs] = np.NaN
31+
32+
self.arr = arr
33+
self.rng = bdate_range(datetime(2009, 1, 1), periods=N)
34+
self.series = Series(arr.copy(), index=self.rng)
35+
self.frame = DataFrame(randn(N, K), index=self.rng,
36+
columns=np.arange(K))
37+
38+
39+
class TestExpanding(Base):
40+
41+
def setup_method(self, method):
42+
self._create_data()
43+
44+
def test_doc_string(self):
45+
46+
df = DataFrame({'B': [0, 1, 2, np.nan, 4]})
47+
df
48+
df.expanding(2).sum()
49+
50+
def test_constructor(self):
51+
# GH 12669
52+
53+
for o in [self.series, self.frame]:
54+
c = o.expanding
55+
56+
# valid
57+
c(min_periods=1)
58+
c(min_periods=1, center=True)
59+
c(min_periods=1, center=False)
60+
61+
# not valid
62+
for w in [2., 'foo', np.array([2])]:
63+
with pytest.raises(ValueError):
64+
c(min_periods=w)
65+
with pytest.raises(ValueError):
66+
c(min_periods=1, center=w)
67+
68+
def test_numpy_compat(self):
69+
# see gh-12811
70+
e = rwindow.Expanding(Series([2, 4, 6]), window=2)
71+
72+
msg = "numpy operations are not valid with window objects"
73+
74+
for func in ('std', 'mean', 'sum', 'max', 'min', 'var'):
75+
tm.assert_raises_regex(UnsupportedFunctionCall, msg,
76+
getattr(e, func), 1, 2, 3)
77+
tm.assert_raises_regex(UnsupportedFunctionCall, msg,
78+
getattr(e, func), dtype=np.float64)
79+
80+
@pytest.mark.parametrize(
81+
'expander',
82+
[1, pytest.param('ls', marks=pytest.mark.xfail(
83+
reason='GH 16425 expanding with '
84+
'offset not supported'))])
85+
def test_empty_df_expanding(self, expander):
86+
# GH 15819 Verifies that datetime and integer expanding windows can be
87+
# applied to empty DataFrames
88+
89+
expected = DataFrame()
90+
result = DataFrame().expanding(expander).sum()
91+
tm.assert_frame_equal(result, expected)
92+
93+
# Verifies that datetime and integer expanding windows can be applied
94+
# to empty DataFrames with datetime index
95+
expected = DataFrame(index=pd.DatetimeIndex([]))
96+
result = DataFrame(
97+
index=pd.DatetimeIndex([])).expanding(expander).sum()
98+
tm.assert_frame_equal(result, expected)
99+
100+
def test_missing_minp_zero(self):
101+
# https://github.com/pandas-dev/pandas/pull/18921
102+
# minp=0
103+
x = pd.Series([np.nan])
104+
result = x.expanding(min_periods=0).sum()
105+
expected = pd.Series([0.0])
106+
tm.assert_series_equal(result, expected)
107+
108+
# minp=1
109+
result = x.expanding(min_periods=1).sum()
110+
expected = pd.Series([np.nan])
111+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)