9
9
import numpy as np
10
10
from distutils .version import LooseVersion
11
11
12
- from pandas import Series , DataFrame , Panel , bdate_range , isnull , notnull
12
+ from pandas import Series , DataFrame , Panel , bdate_range , isnull , notnull , concat
13
13
from pandas .util .testing import (
14
14
assert_almost_equal , assert_series_equal , assert_frame_equal , assert_panel_equal , assert_index_equal
15
15
)
20
20
21
21
N , K = 100 , 10
22
22
23
-
24
- class TestMoments (tm .TestCase ):
23
+ class Base (tm .TestCase ):
25
24
26
25
_multiprocess_can_split_ = True
27
26
28
27
_nan_locs = np .arange (20 , 40 )
29
28
_inf_locs = np .array ([])
30
29
31
- def setUp (self ):
30
+ def _create_data (self ):
32
31
arr = randn (N )
33
32
arr [self ._nan_locs ] = np .NaN
34
33
@@ -40,6 +39,10 @@ def setUp(self):
40
39
self .frame = DataFrame (randn (N , K ), index = self .rng ,
41
40
columns = np .arange (K ))
42
41
42
+ class TestMoments (Base ):
43
+
44
+ def setUp (self ):
45
+ self ._create_data ()
43
46
warnings .simplefilter ("ignore" , category = FutureWarning )
44
47
45
48
def test_centered_axis_validation (self ):
@@ -71,7 +74,7 @@ def test_cmov_mean(self):
71
74
# GH 8238
72
75
tm ._skip_if_no_scipy ()
73
76
74
- vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 , 13.49 ,
77
+ vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 , 13.49 ,
75
78
16.68 , 9.48 , 10.63 , 14.48 ])
76
79
xp = np .array ([np .nan , np .nan , 9.962 , 11.27 , 11.564 , 12.516 ,
77
80
12.818 , 12.952 , np .nan , np .nan ])
@@ -87,7 +90,7 @@ def test_cmov_window(self):
87
90
# GH 8238
88
91
tm ._skip_if_no_scipy ()
89
92
90
- vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 ,
93
+ vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 ,
91
94
13.49 , 16.68 , 9.48 , 10.63 , 14.48 ])
92
95
xp = np .array ([np .nan , np .nan , 9.962 , 11.27 , 11.564 , 12.516 ,
93
96
12.818 , 12.952 , np .nan , np .nan ])
@@ -173,21 +176,21 @@ def test_cmov_window_regular(self):
173
176
vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 ,
174
177
13.49 , 16.68 , 9.48 , 10.63 , 14.48 ])
175
178
xps = {
176
- 'hamming' : [np .nan , np .nan , 8.71384 , 9.56348 , 12.38009 ,
177
- 14.03687 , 13.8567 , 11.81473 , np .nan , np .nan ],
178
- 'triang' : [np .nan , np .nan , 9.28667 , 10.34667 , 12.00556 ,
179
- 13.33889 , 13.38 , 12.33667 , np .nan , np .nan ],
180
- 'barthann' : [np .nan , np .nan , 8.4425 , 9.1925 , 12.5575 ,
181
- 14.3675 , 14.0825 , 11.5675 , np .nan , np .nan ],
182
- 'bohman' : [np .nan , np .nan , 7.61599 , 9.1764 , 12.83559 ,
183
- 14.17267 , 14.65923 , 11.10401 , np .nan , np .nan ],
184
- 'blackmanharris' : [np .nan , np .nan , 6.97691 , 9.16438 , 13.05052 ,
179
+ 'hamming' : [np .nan , np .nan , 8.71384 , 9.56348 , 12.38009 ,
180
+ 14.03687 , 13.8567 , 11.81473 , np .nan , np .nan ],
181
+ 'triang' : [np .nan , np .nan , 9.28667 , 10.34667 , 12.00556 ,
182
+ 13.33889 , 13.38 , 12.33667 , np .nan , np .nan ],
183
+ 'barthann' : [np .nan , np .nan , 8.4425 , 9.1925 , 12.5575 ,
184
+ 14.3675 , 14.0825 , 11.5675 , np .nan , np .nan ],
185
+ 'bohman' : [np .nan , np .nan , 7.61599 , 9.1764 , 12.83559 ,
186
+ 14.17267 , 14.65923 , 11.10401 , np .nan , np .nan ],
187
+ 'blackmanharris' : [np .nan , np .nan , 6.97691 , 9.16438 , 13.05052 ,
185
188
14.02156 , 15.10512 , 10.74574 , np .nan , np .nan ],
186
- 'nuttall' : [np .nan , np .nan , 7.04618 , 9.16786 , 13.02671 ,
187
- 14.03559 , 15.05657 , 10.78514 , np .nan , np .nan ],
188
- 'blackman' : [np .nan , np .nan , 7.73345 , 9.17869 , 12.79607 ,
189
- 14.20036 , 14.57726 , 11.16988 , np .nan , np .nan ],
190
- 'bartlett' : [np .nan , np .nan , 8.4425 , 9.1925 , 12.5575 ,
189
+ 'nuttall' : [np .nan , np .nan , 7.04618 , 9.16786 , 13.02671 ,
190
+ 14.03559 , 15.05657 , 10.78514 , np .nan , np .nan ],
191
+ 'blackman' : [np .nan , np .nan , 7.73345 , 9.17869 , 12.79607 ,
192
+ 14.20036 , 14.57726 , 11.16988 , np .nan , np .nan ],
193
+ 'bartlett' : [np .nan , np .nan , 8.4425 , 9.1925 , 12.5575 ,
191
194
14.3675 , 14.0825 , 11.5675 , np .nan , np .nan ]}
192
195
193
196
for wt in win_types :
@@ -219,25 +222,25 @@ def test_cmov_window_regular_missing_data(self):
219
222
win_types = ['triang' , 'blackman' , 'hamming' , 'bartlett' , 'bohman' ,
220
223
'blackmanharris' , 'nuttall' , 'barthann' ]
221
224
222
- vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 ,
225
+ vals = np .array ([6.95 , 15.21 , 4.72 , 9.12 , 13.81 ,
223
226
13.49 , 16.68 , np .nan , 10.63 , 14.48 ])
224
227
xps = {
225
- 'bartlett' : [np .nan , np .nan , 9.70333 , 10.5225 , 8.4425 ,
226
- 9.1925 , 12.5575 , 14.3675 , 15.61667 , 13.655 ],
227
- 'blackman' : [np .nan , np .nan , 9.04582 , 11.41536 , 7.73345 ,
228
- 9.17869 , 12.79607 , 14.20036 , 15.8706 , 13.655 ],
229
- 'barthann' : [np .nan , np .nan , 9.70333 , 10.5225 , 8.4425 ,
230
- 9.1925 , 12.5575 , 14.3675 , 15.61667 , 13.655 ],
231
- 'bohman' : [np .nan , np .nan , 8.9444 , 11.56327 , 7.61599 ,
232
- 9.1764 , 12.83559 , 14.17267 , 15.90976 , 13.655 ],
233
- 'hamming' : [np .nan , np .nan , 9.59321 , 10.29694 , 8.71384 ,
234
- 9.56348 , 12.38009 , 14.20565 , 15.24694 , 13.69758 ],
235
- 'nuttall' : [np .nan , np .nan , 8.47693 , 12.2821 , 7.04618 ,
236
- 9.16786 , 13.02671 , 14.03673 , 16.08759 , 13.65553 ],
237
- 'triang' : [np .nan , np .nan , 9.33167 , 9.76125 , 9.28667 ,
238
- 10.34667 , 12.00556 , 13.82125 , 14.49429 , 13.765 ],
228
+ 'bartlett' : [np .nan , np .nan , 9.70333 , 10.5225 , 8.4425 ,
229
+ 9.1925 , 12.5575 , 14.3675 , 15.61667 , 13.655 ],
230
+ 'blackman' : [np .nan , np .nan , 9.04582 , 11.41536 , 7.73345 ,
231
+ 9.17869 , 12.79607 , 14.20036 , 15.8706 , 13.655 ],
232
+ 'barthann' : [np .nan , np .nan , 9.70333 , 10.5225 , 8.4425 ,
233
+ 9.1925 , 12.5575 , 14.3675 , 15.61667 , 13.655 ],
234
+ 'bohman' : [np .nan , np .nan , 8.9444 , 11.56327 , 7.61599 ,
235
+ 9.1764 , 12.83559 , 14.17267 , 15.90976 , 13.655 ],
236
+ 'hamming' : [np .nan , np .nan , 9.59321 , 10.29694 , 8.71384 ,
237
+ 9.56348 , 12.38009 , 14.20565 , 15.24694 , 13.69758 ],
238
+ 'nuttall' : [np .nan , np .nan , 8.47693 , 12.2821 , 7.04618 ,
239
+ 9.16786 , 13.02671 , 14.03673 , 16.08759 , 13.65553 ],
240
+ 'triang' : [np .nan , np .nan , 9.33167 , 9.76125 , 9.28667 ,
241
+ 10.34667 , 12.00556 , 13.82125 , 14.49429 , 13.765 ],
239
242
'blackmanharris' : [np .nan , np .nan , 8.42526 , 12.36824 , 6.97691 ,
240
- 9.16438 , 13.05052 , 14.02175 , 16.1098 ,
243
+ 9.16438 , 13.05052 , 14.02175 , 16.1098 ,
241
244
13.65509 ]
242
245
}
243
246
@@ -258,14 +261,14 @@ def test_cmov_window_special(self):
258
261
13.49 , 16.68 , 9.48 , 10.63 , 14.48 ])
259
262
260
263
xps = {
261
- 'gaussian' : [np .nan , np .nan , 8.97297 , 9.76077 , 12.24763 ,
262
- 13.89053 , 13.65671 , 12.01002 , np .nan , np .nan ],
263
- 'general_gaussian' : [np .nan , np .nan , 9.85011 , 10.71589 ,
264
- 11.73161 , 13.08516 , 12.95111 , 12.74577 ,
265
- np .nan , np .nan ],
266
- 'slepian' : [np .nan , np .nan , 9.81073 , 10.89359 , 11.70284 ,
267
- 12.88331 , 12.96079 , 12.77008 , np .nan , np .nan ],
268
- 'kaiser' : [np .nan , np .nan , 9.86851 , 11.02969 , 11.65161 ,
264
+ 'gaussian' : [np .nan , np .nan , 8.97297 , 9.76077 , 12.24763 ,
265
+ 13.89053 , 13.65671 , 12.01002 , np .nan , np .nan ],
266
+ 'general_gaussian' : [np .nan , np .nan , 9.85011 , 10.71589 ,
267
+ 11.73161 , 13.08516 , 12.95111 , 12.74577 ,
268
+ np .nan , np .nan ],
269
+ 'slepian' : [np .nan , np .nan , 9.81073 , 10.89359 , 11.70284 ,
270
+ 12.88331 , 12.96079 , 12.77008 , np .nan , np .nan ],
271
+ 'kaiser' : [np .nan , np .nan , 9.86851 , 11.02969 , 11.65161 ,
269
272
12.75129 , 12.90702 , 12.83757 , np .nan , np .nan ]
270
273
}
271
274
@@ -635,7 +638,7 @@ def test_ewma(self):
635
638
self .assertTrue (np .abs (result - 1 ) < 1e-2 )
636
639
637
640
s = Series ([1.0 , 2.0 , 4.0 , 8.0 ])
638
-
641
+
639
642
expected = Series ([1.0 , 1.6 , 2.736842 , 4.923077 ])
640
643
for f in [lambda s : mom .ewma (s , com = 2.0 , adjust = True ),
641
644
lambda s : mom .ewma (s , com = 2.0 , adjust = True , ignore_na = False ),
@@ -783,7 +786,10 @@ def _check_ew_structures(self, func):
783
786
frame_result = func (self .frame , com = 10 )
784
787
self .assertEqual (type (frame_result ), DataFrame )
785
788
786
- def _test_series (self ):
789
+ # create the data only once as we are not setting it
790
+ def _create_consistency_data ():
791
+
792
+ def create_series ():
787
793
return [Series (),
788
794
Series ([np .nan ]),
789
795
Series ([np .nan , np .nan ]),
@@ -804,18 +810,38 @@ def _test_series(self):
804
810
Series (range (20 , 0 , - 2 )),
805
811
]
806
812
807
- def _test_dataframes ( self ):
813
+ def create_dataframes ( ):
808
814
return [DataFrame (),
809
815
DataFrame (columns = ['a' ]),
810
816
DataFrame (columns = ['a' , 'a' ]),
811
817
DataFrame (columns = ['a' , 'b' ]),
812
818
DataFrame (np .arange (10 ).reshape ((5 , 2 ))),
813
819
DataFrame (np .arange (25 ).reshape ((5 , 5 ))),
814
820
DataFrame (np .arange (25 ).reshape ((5 , 5 )), columns = ['a' , 'b' , 99 , 'd' , 'd' ]),
815
- ] + [DataFrame (s ) for s in self ._test_series ()]
821
+ ] + [DataFrame (s ) for s in create_series ()]
822
+
823
+ def is_constant (x ):
824
+ values = x .values .ravel ()
825
+ return len (set (values [notnull (values )])) == 1
826
+
827
+ def no_nans (x ):
828
+ return x .notnull ().all ().all ()
829
+
830
+ # data is a tuple(object, is_contant, no_nans)
831
+ data = create_series () + create_dataframes ()
832
+
833
+ return [ (x , is_constant (x ), no_nans (x )) for x in data ]
834
+ _consistency_data = _create_consistency_data ()
816
835
817
- def _test_data (self ):
818
- return self ._test_series () + self ._test_dataframes ()
836
+ class TestMomentsConsistency (Base ):
837
+
838
+ def _create_data (self ):
839
+ super (TestMomentsConsistency , self )._create_data ()
840
+ self .data = _consistency_data
841
+
842
+ def setUp (self ):
843
+ self ._create_data ()
844
+ warnings .simplefilter ("ignore" , category = FutureWarning )
819
845
820
846
def _test_moments_consistency (self ,
821
847
min_periods ,
@@ -825,11 +851,11 @@ def _test_moments_consistency(self,
825
851
var_debiasing_factors = None ):
826
852
827
853
def _non_null_values (x ):
828
- return set ([v for v in x .values .reshape (x .values .size ) if notnull (v )])
854
+ values = x .values .ravel ()
855
+ return set (values [notnull (values )].tolist ())
829
856
830
- for x in self ._test_data () :
857
+ for ( x , is_constant , no_nans ) in self .data :
831
858
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
832
- is_constant = (len (_non_null_values (x )) == 1 )
833
859
count_x = count (x )
834
860
mean_x = mean (x )
835
861
@@ -861,7 +887,7 @@ def _non_null_values(x):
861
887
862
888
for (std , var , cov ) in [(std_biased , var_biased , cov_biased ),
863
889
(std_unbiased , var_unbiased , cov_unbiased )]:
864
-
890
+
865
891
# check that var(x), std(x), and cov(x) are all >= 0
866
892
var_x = var (x )
867
893
std_x = std (x )
@@ -873,7 +899,7 @@ def _non_null_values(x):
873
899
874
900
# check that var(x) == cov(x, x)
875
901
assert_equal (var_x , cov_x_x )
876
-
902
+
877
903
# check that var(x) == std(x)^2
878
904
assert_equal (var_x , std_x * std_x )
879
905
@@ -892,7 +918,7 @@ def _non_null_values(x):
892
918
assert_equal (var_x , expected )
893
919
894
920
if isinstance (x , Series ):
895
- for y in self ._test_data () :
921
+ for ( y , is_constant , no_nans ) in self .data :
896
922
if not x .isnull ().equals (y .isnull ()):
897
923
# can only easily test two Series with similar structure
898
924
continue
@@ -907,7 +933,7 @@ def _non_null_values(x):
907
933
cov_x_y = cov (x , y )
908
934
cov_y_x = cov (y , x )
909
935
assert_equal (cov_x_y , cov_y_x )
910
-
936
+
911
937
# check that cov(x, y) == (var(x+y) - var(x) - var(y)) / 2
912
938
var_x_plus_y = var (x + y )
913
939
var_y = var (y )
@@ -928,9 +954,15 @@ def test_ewm_consistency(self):
928
954
929
955
def _weights (s , com , adjust , ignore_na ):
930
956
if isinstance (s , DataFrame ):
931
- w = DataFrame (index = s .index , columns = s .columns )
932
- for i , _ in enumerate (s .columns ):
933
- w .iloc [:, i ] = _weights (s .iloc [:, i ], com = com , adjust = adjust , ignore_na = ignore_na )
957
+ if not len (s .columns ):
958
+ return DataFrame (index = s .index , columns = s .columns )
959
+ w = concat ([ _weights (s .iloc [:, i ],
960
+ com = com ,
961
+ adjust = adjust ,
962
+ ignore_na = ignore_na ) for i , _ in enumerate (s .columns ) ],
963
+ axis = 1 )
964
+ w .index = s .index
965
+ w .columns = s .columns
934
966
return w
935
967
936
968
w = Series (np .nan , index = s .index )
@@ -1053,11 +1085,12 @@ def test_expanding_consistency(self):
1053
1085
1054
1086
# test consistency between expanding_xyz() and either (a) expanding_apply of Series.xyz(),
1055
1087
# or (b) expanding_apply of np.nanxyz()
1056
- for x in self ._test_data () :
1088
+ for ( x , is_constant , no_nans ) in self .data :
1057
1089
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
1058
1090
functions = base_functions
1091
+
1059
1092
# GH 8269
1060
- if x . notnull (). all (). all () :
1093
+ if no_nans :
1061
1094
functions = base_functions + no_nan_functions
1062
1095
for (expanding_f , f , require_min_periods ) in functions :
1063
1096
if require_min_periods and (min_periods is not None ) and (min_periods < require_min_periods ):
@@ -1085,6 +1118,7 @@ def test_expanding_consistency(self):
1085
1118
1086
1119
@slow
1087
1120
def test_rolling_consistency (self ):
1121
+
1088
1122
base_functions = [
1089
1123
(mom .rolling_count , lambda v : Series (v ).count (), None ),
1090
1124
(mom .rolling_max , lambda v : Series (v ).max (), None ),
@@ -1150,11 +1184,12 @@ def test_rolling_consistency(self):
1150
1184
1151
1185
# test consistency between rolling_xyz() and either (a) rolling_apply of Series.xyz(),
1152
1186
# or (b) rolling_apply of np.nanxyz()
1153
- for x in self ._test_data ():
1187
+ for (x , is_constant , no_nans ) in self .data :
1188
+
1154
1189
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
1155
1190
functions = base_functions
1156
1191
# GH 8269
1157
- if x . notnull (). all (). all () :
1192
+ if no_nans :
1158
1193
functions = base_functions + no_nan_functions
1159
1194
for (rolling_f , f , require_min_periods ) in functions :
1160
1195
if require_min_periods and (min_periods is not None ) and (min_periods < require_min_periods ):
@@ -1183,7 +1218,7 @@ def test_rolling_consistency(self):
1183
1218
expected .iloc [:, i , j ] = rolling_f (x .iloc [:, i ], x .iloc [:, j ],
1184
1219
window = window , min_periods = min_periods , center = center )
1185
1220
assert_panel_equal (rolling_f_result , expected )
1186
-
1221
+
1187
1222
# binary moments
1188
1223
def test_rolling_cov (self ):
1189
1224
A = self .series
@@ -1608,7 +1643,7 @@ def test_expanding_corr_pairwise_diff_length(self):
1608
1643
assert_frame_equal (result2 , expected )
1609
1644
assert_frame_equal (result3 , expected )
1610
1645
assert_frame_equal (result4 , expected )
1611
-
1646
+
1612
1647
def test_pairwise_stats_column_names_order (self ):
1613
1648
# GH 7738
1614
1649
df1s = [DataFrame ([[2 ,4 ],[1 ,2 ],[5 ,2 ],[8 ,1 ]], columns = [0 ,1 ]),
0 commit comments