3
3
import pytest
4
4
5
5
from pandas import DataFrame , Series , concat
6
+ import pandas ._testing as tm
6
7
from pandas .tests .window .common import (
7
- check_binary_ew ,
8
- check_binary_ew_min_periods ,
9
- check_pairwise_moment ,
10
- ew_func ,
11
8
moments_consistency_cov_data ,
12
9
moments_consistency_is_constant ,
13
10
moments_consistency_mock_mean ,
20
17
21
18
@pytest .mark .parametrize ("func" , ["cov" , "corr" ])
22
19
def test_ewm_pairwise_cov_corr (func , frame ):
23
- check_pairwise_moment (frame , "ewm" , func , span = 10 , min_periods = 5 )
20
+ result = getattr (frame .ewm (span = 10 , min_periods = 5 ), func )()
21
+ result = result .loc [(slice (None ), 1 ), 5 ]
22
+ result .index = result .index .droplevel (1 )
23
+ expected = getattr (frame [1 ].ewm (span = 10 , min_periods = 5 ), func )(frame [5 ])
24
+ expected .index = expected .index ._with_freq (None )
25
+ tm .assert_series_equal (result , expected , check_names = False )
24
26
25
27
26
28
@pytest .mark .parametrize ("name" , ["cov" , "corr" ])
27
- def test_ewm_corr_cov (name , min_periods , binary_ew_data ):
29
+ def test_ewm_corr_cov (name , binary_ew_data ):
28
30
A , B = binary_ew_data
29
31
30
- check_binary_ew (name = "corr" , A = A , B = B )
31
- check_binary_ew_min_periods ("corr" , min_periods , A , B )
32
+ result = getattr (A .ewm (com = 20 , min_periods = 5 ), name )(B )
33
+ assert np .isnan (result .values [:14 ]).all ()
34
+ assert not np .isnan (result .values [14 :]).any ()
35
+
36
+
37
+ @pytest .mark .parametrize ("name" , ["cov" , "corr" ])
38
+ def test_ewm_corr_cov_min_periods (name , min_periods , binary_ew_data ):
39
+ # GH 7898
40
+ A , B = binary_ew_data
41
+ result = getattr (A .ewm (com = 20 , min_periods = min_periods ), name )(B )
42
+ # binary functions (ewmcov, ewmcorr) with bias=False require at
43
+ # least two values
44
+ assert np .isnan (result .values [:11 ]).all ()
45
+ assert not np .isnan (result .values [11 :]).any ()
46
+
47
+ # check series of length 0
48
+ empty = Series ([], dtype = np .float64 )
49
+ result = getattr (empty .ewm (com = 50 , min_periods = min_periods ), name )(empty )
50
+ tm .assert_series_equal (result , empty )
51
+
52
+ # check series of length 1
53
+ result = getattr (Series ([1.0 ]).ewm (com = 50 , min_periods = min_periods ), name )(
54
+ Series ([1.0 ])
55
+ )
56
+ tm .assert_series_equal (result , Series ([np .NaN ]))
32
57
33
58
34
59
@pytest .mark .parametrize ("name" , ["cov" , "corr" ])
@@ -38,7 +63,7 @@ def test_different_input_array_raise_exception(name, binary_ew_data):
38
63
msg = "Input arrays must be of the same type!"
39
64
# exception raised is Exception
40
65
with pytest .raises (Exception , match = msg ):
41
- ew_func ( A , randn ( 50 ), 20 , name = name , min_periods = 5 )
66
+ getattr ( A . ewm ( com = 20 , min_periods = 5 ), name )( randn ( 50 ) )
42
67
43
68
44
69
@pytest .mark .slow
0 commit comments