8
8
from pandas .core .api import Series , DataFrame , DateRange
9
9
from pandas .util .testing import assert_almost_equal
10
10
import pandas .core .datetools as datetools
11
- import pandas .stats .moments as moments
11
+ import pandas .stats .moments as mom
12
12
import pandas .util .testing as tm
13
13
14
14
N , K = 100 , 10
@@ -31,25 +31,25 @@ def setUp(self):
31
31
columns = np .arange (K ))
32
32
33
33
def test_rolling_sum (self ):
34
- self ._check_moment_func (moments .rolling_sum , np .sum )
34
+ self ._check_moment_func (mom .rolling_sum , np .sum )
35
35
36
36
def test_rolling_count (self ):
37
37
counter = lambda x : np .isfinite (x ).astype (float ).sum ()
38
- self ._check_moment_func (moments .rolling_count , counter ,
38
+ self ._check_moment_func (mom .rolling_count , counter ,
39
39
has_min_periods = False ,
40
40
preserve_nan = False )
41
41
42
42
def test_rolling_mean (self ):
43
- self ._check_moment_func (moments .rolling_mean , np .mean )
43
+ self ._check_moment_func (mom .rolling_mean , np .mean )
44
44
45
45
def test_rolling_median (self ):
46
- self ._check_moment_func (moments .rolling_median , np .median )
46
+ self ._check_moment_func (mom .rolling_median , np .median )
47
47
48
48
def test_rolling_min (self ):
49
- self ._check_moment_func (moments .rolling_min , np .min )
49
+ self ._check_moment_func (mom .rolling_min , np .min )
50
50
51
51
def test_rolling_max (self ):
52
- self ._check_moment_func (moments .rolling_max , np .max )
52
+ self ._check_moment_func (mom .rolling_max , np .max )
53
53
54
54
def test_rolling_quantile (self ):
55
55
qs = [.1 , .5 , .9 ]
@@ -62,7 +62,7 @@ def scoreatpercentile(a, per):
62
62
63
63
for q in qs :
64
64
def f (x , window , min_periods = None , time_rule = None ):
65
- return moments .rolling_quantile (x , window , q ,
65
+ return mom .rolling_quantile (x , window , q ,
66
66
min_periods = min_periods ,
67
67
time_rule = time_rule )
68
68
def alt (x ):
@@ -72,34 +72,34 @@ def alt(x):
72
72
73
73
def test_rolling_apply (self ):
74
74
def roll_mean (x , window , min_periods = None , time_rule = None ):
75
- return moments .rolling_apply (x , window ,
75
+ return mom .rolling_apply (x , window ,
76
76
lambda x : x [np .isfinite (x )].mean (),
77
77
min_periods = min_periods ,
78
78
time_rule = time_rule )
79
79
self ._check_moment_func (roll_mean , np .mean )
80
80
81
81
def test_rolling_std (self ):
82
- self ._check_moment_func (moments .rolling_std ,
82
+ self ._check_moment_func (mom .rolling_std ,
83
83
lambda x : np .std (x , ddof = 1 ))
84
84
85
85
def test_rolling_var (self ):
86
- self ._check_moment_func (moments .rolling_var ,
86
+ self ._check_moment_func (mom .rolling_var ,
87
87
lambda x : np .var (x , ddof = 1 ))
88
88
89
89
def test_rolling_skew (self ):
90
90
try :
91
91
from scipy .stats import skew
92
92
except ImportError :
93
93
raise nose .SkipTest ('no scipy' )
94
- self ._check_moment_func (moments .rolling_skew ,
94
+ self ._check_moment_func (mom .rolling_skew ,
95
95
lambda x : skew (x , bias = False ))
96
96
97
97
def test_rolling_kurt (self ):
98
98
try :
99
99
from scipy .stats import kurtosis
100
100
except ImportError :
101
101
raise nose .SkipTest ('no scipy' )
102
- self ._check_moment_func (moments .rolling_kurt ,
102
+ self ._check_moment_func (mom .rolling_kurt ,
103
103
lambda x : kurtosis (x , bias = False ))
104
104
105
105
def _check_moment_func (self , func , static_comp , window = 50 ,
@@ -186,21 +186,21 @@ def _check_structures(self, func, static_comp,
186
186
trunc_frame .apply (static_comp ))
187
187
188
188
def test_ewma (self ):
189
- self ._check_ew (moments .ewma )
189
+ self ._check_ew (mom .ewma )
190
190
191
191
def test_ewmvar (self ):
192
- self ._check_ew (moments .ewmvar )
192
+ self ._check_ew (mom .ewmvar )
193
193
194
194
def test_ewmvol (self ):
195
- self ._check_ew (moments .ewmvol )
195
+ self ._check_ew (mom .ewmvol )
196
196
197
197
def test_ewma_span_com_args (self ):
198
- A = moments .ewma (self .arr , com = 9.5 )
199
- B = moments .ewma (self .arr , span = 20 )
198
+ A = mom .ewma (self .arr , com = 9.5 )
199
+ B = mom .ewma (self .arr , span = 20 )
200
200
assert_almost_equal (A , B )
201
201
202
- self .assertRaises (Exception , moments .ewma , self .arr , com = 9.5 , span = 20 )
203
- self .assertRaises (Exception , moments .ewma , self .arr )
202
+ self .assertRaises (Exception , mom .ewma , self .arr , com = 9.5 , span = 20 )
203
+ self .assertRaises (Exception , mom .ewma , self .arr )
204
204
205
205
def _check_ew (self , func ):
206
206
self ._check_ew_ndarray (func )
@@ -233,14 +233,14 @@ def test_rolling_cov(self):
233
233
A = self .series
234
234
B = A + randn (len (A ))
235
235
236
- result = moments .rolling_cov (A , B , 50 , min_periods = 25 )
236
+ result = mom .rolling_cov (A , B , 50 , min_periods = 25 )
237
237
assert_almost_equal (result [- 1 ], np .cov (A [- 50 :], B [- 50 :])[0 , 1 ])
238
238
239
239
def test_rolling_corr (self ):
240
240
A = self .series
241
241
B = A + randn (len (A ))
242
242
243
- result = moments .rolling_corr (A , B , 50 , min_periods = 25 )
243
+ result = mom .rolling_corr (A , B , 50 , min_periods = 25 )
244
244
assert_almost_equal (result [- 1 ], np .corrcoef (A [- 50 :], B [- 50 :])[0 , 1 ])
245
245
246
246
# test for correct bias correction
@@ -249,14 +249,37 @@ def test_rolling_corr(self):
249
249
a [:5 ] = np .nan
250
250
b [:10 ] = np .nan
251
251
252
- result = moments .rolling_corr (a , b , len (a ), min_periods = 1 )
252
+ result = mom .rolling_corr (a , b , len (a ), min_periods = 1 )
253
253
assert_almost_equal (result [- 1 ], a .corr (b ))
254
254
255
+ def test_flex_binary_frame (self ):
256
+ def _check (method ):
257
+ series = self .frame [1 ]
258
+
259
+ res = method (series , self .frame , 10 )
260
+ res2 = method (self .frame , series , 10 )
261
+ exp = self .frame .apply (lambda x : method (series , x , 10 ))
262
+
263
+ tm .assert_frame_equal (res , exp )
264
+ tm .assert_frame_equal (res2 , exp )
265
+
266
+ frame2 = self .frame .copy ()
267
+ frame2 .values [:] = np .random .randn (* frame2 .shape )
268
+
269
+ res3 = method (self .frame , frame2 , 10 )
270
+ exp = DataFrame (dict ((k , method (self .frame [k ], frame2 [k ], 10 ))
271
+ for k in self .frame ))
272
+ tm .assert_frame_equal (res3 , exp )
273
+
274
+ methods = [mom .rolling_corr , mom .rolling_cov ]
275
+ for meth in methods :
276
+ _check (meth )
277
+
255
278
def test_ewmcov (self ):
256
- self ._check_binary_ew (moments .ewmcov )
279
+ self ._check_binary_ew (mom .ewmcov )
257
280
258
281
def test_ewmcorr (self ):
259
- self ._check_binary_ew (moments .ewmcorr )
282
+ self ._check_binary_ew (mom .ewmcorr )
260
283
261
284
def _check_binary_ew (self , func ):
262
285
A = Series (randn (50 ), index = np .arange (50 ))
0 commit comments