@@ -842,6 +842,44 @@ def no_nans(x):
842
842
_consistency_data = _create_consistency_data ()
843
843
844
844
class TestMomentsConsistency (Base ):
845
+ base_functions = [
846
+ (lambda v : Series (v ).count (), None , 'count' ),
847
+ (lambda v : Series (v ).max (), None , 'max' ),
848
+ (lambda v : Series (v ).min (), None , 'min' ),
849
+ (lambda v : Series (v ).sum (), None , 'sum' ),
850
+ (lambda v : Series (v ).mean (), None , 'mean' ),
851
+ (lambda v : Series (v ).std (), 1 , 'std' ),
852
+ (lambda v : Series (v ).cov (Series (v )), None , 'cov' ),
853
+ (lambda v : Series (v ).corr (Series (v )), None , 'corr' ),
854
+ (lambda v : Series (v ).var (), 1 , 'var' ),
855
+ #(lambda v: Series(v).skew(), 3, 'skew'), # restore once GH 8086 is fixed
856
+ #(lambda v: Series(v).kurt(), 4, 'kurt'), # restore once GH 8086 is fixed
857
+ #(lambda x, min_periods: mom.expanding_quantile(x, 0.3, min_periods=min_periods, 'quantile'),
858
+ # lambda v: Series(v).quantile(0.3), None, 'quantile'), # restore once GH 8084 is fixed
859
+ (lambda v : Series (v ).median (), None ,'median' ),
860
+ (np .nanmax , 1 , 'max' ),
861
+ (np .nanmin , 1 , 'min' ),
862
+ (np .nansum , 1 , 'sum' ),
863
+ ]
864
+ if np .__version__ >= LooseVersion ('1.8.0' ):
865
+ base_functions += [
866
+ (np .nanmean , 1 , 'mean' ),
867
+ (lambda v : np .nanstd (v , ddof = 1 ), 1 ,'std' ),
868
+ (lambda v : np .nanvar (v , ddof = 1 ), 1 ,'var' ),
869
+ ]
870
+ if np .__version__ >= LooseVersion ('1.9.0' ):
871
+ base_functions += [
872
+ (np .nanmedian , 1 , 'median' ),
873
+ ]
874
+ no_nan_functions = [
875
+ (np .max , None , 'max' ),
876
+ (np .min , None , 'min' ),
877
+ (np .sum , None , 'sum' ),
878
+ (np .mean , None , 'mean' ),
879
+ (lambda v : np .std (v , ddof = 1 ), 1 ,'std' ),
880
+ (lambda v : np .var (v , ddof = 1 ), 1 ,'var' ),
881
+ (np .median , None , 'median' ),
882
+ ]
845
883
846
884
def _create_data (self ):
847
885
super (TestMomentsConsistency , self )._create_data ()
@@ -877,9 +915,11 @@ def _non_null_values(x):
877
915
# self.assertTrue(_non_null_values(corr_x_x).issubset(set([1.]))) # restore once rolling_cov(x, x) is identically equal to var(x)
878
916
879
917
if is_constant :
918
+ exp = x .max () if isinstance (x , Series ) else x .max ().max ()
919
+
880
920
# check mean of constant series
881
921
expected = x * np .nan
882
- expected [count_x >= max (min_periods , 1 )] = x . max (). max ()
922
+ expected [count_x >= max (min_periods , 1 )] = exp
883
923
assert_equal (mean_x , expected )
884
924
885
925
# check correlation of constant series with itself is NaN
@@ -1030,44 +1070,6 @@ def _ewma(s, com, min_periods, adjust, ignore_na):
1030
1070
1031
1071
@slow
1032
1072
def test_expanding_consistency (self ):
1033
- base_functions = [
1034
- (mom .expanding_count , lambda v : Series (v ).count (), None ),
1035
- (mom .expanding_max , lambda v : Series (v ).max (), None ),
1036
- (mom .expanding_min , lambda v : Series (v ).min (), None ),
1037
- (mom .expanding_sum , lambda v : Series (v ).sum (), None ),
1038
- (mom .expanding_mean , lambda v : Series (v ).mean (), None ),
1039
- (mom .expanding_std , lambda v : Series (v ).std (), 1 ),
1040
- (mom .expanding_cov , lambda v : Series (v ).cov (Series (v )), None ),
1041
- (mom .expanding_corr , lambda v : Series (v ).corr (Series (v )), None ),
1042
- (mom .expanding_var , lambda v : Series (v ).var (), 1 ),
1043
- #(mom.expanding_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed
1044
- #(mom.expanding_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed
1045
- #(lambda x, min_periods: mom.expanding_quantile(x, 0.3, min_periods=min_periods),
1046
- # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed
1047
- (mom .expanding_median , lambda v : Series (v ).median (), None ),
1048
- (mom .expanding_max , np .nanmax , 1 ),
1049
- (mom .expanding_min , np .nanmin , 1 ),
1050
- (mom .expanding_sum , np .nansum , 1 ),
1051
- ]
1052
- if np .__version__ >= LooseVersion ('1.8.0' ):
1053
- base_functions += [
1054
- (mom .expanding_mean , np .nanmean , 1 ),
1055
- (mom .expanding_std , lambda v : np .nanstd (v , ddof = 1 ), 1 ),
1056
- (mom .expanding_var , lambda v : np .nanvar (v , ddof = 1 ), 1 ),
1057
- ]
1058
- if np .__version__ >= LooseVersion ('1.9.0' ):
1059
- base_functions += [
1060
- (mom .expanding_median , np .nanmedian , 1 ),
1061
- ]
1062
- no_nan_functions = [
1063
- (mom .expanding_max , np .max , None ),
1064
- (mom .expanding_min , np .min , None ),
1065
- (mom .expanding_sum , np .sum , None ),
1066
- (mom .expanding_mean , np .mean , None ),
1067
- (mom .expanding_std , lambda v : np .std (v , ddof = 1 ), 1 ),
1068
- (mom .expanding_var , lambda v : np .var (v , ddof = 1 ), 1 ),
1069
- (mom .expanding_median , np .median , None ),
1070
- ]
1071
1073
1072
1074
# suppress warnings about empty slices, as we are deliberately testing with empty/0-length Series/DataFrames
1073
1075
with warnings .catch_warnings ():
@@ -1095,12 +1097,14 @@ def test_expanding_consistency(self):
1095
1097
# or (b) expanding_apply of np.nanxyz()
1096
1098
for (x , is_constant , no_nans ) in self .data :
1097
1099
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
1098
- functions = base_functions
1100
+ functions = self . base_functions
1099
1101
1100
1102
# GH 8269
1101
1103
if no_nans :
1102
- functions = base_functions + no_nan_functions
1103
- for (expanding_f , f , require_min_periods ) in functions :
1104
+ functions = self .base_functions + self .no_nan_functions
1105
+ for (f , require_min_periods , name ) in functions :
1106
+ expanding_f = getattr (mom ,'expanding_{0}' .format (name ))
1107
+
1104
1108
if require_min_periods and (min_periods is not None ) and (min_periods < require_min_periods ):
1105
1109
continue
1106
1110
@@ -1113,7 +1117,9 @@ def test_expanding_consistency(self):
1113
1117
else :
1114
1118
expanding_f_result = expanding_f (x , min_periods = min_periods )
1115
1119
expanding_apply_f_result = mom .expanding_apply (x , func = f , min_periods = min_periods )
1116
- assert_equal (expanding_f_result , expanding_apply_f_result )
1120
+
1121
+ if not tm ._incompat_bottleneck_version (name ):
1122
+ assert_equal (expanding_f_result , expanding_apply_f_result )
1117
1123
1118
1124
if (expanding_f in [mom .expanding_cov , mom .expanding_corr ]) and isinstance (x , DataFrame ):
1119
1125
# test pairwise=True
@@ -1127,45 +1133,6 @@ def test_expanding_consistency(self):
1127
1133
@slow
1128
1134
def test_rolling_consistency (self ):
1129
1135
1130
- base_functions = [
1131
- (mom .rolling_count , lambda v : Series (v ).count (), None ),
1132
- (mom .rolling_max , lambda v : Series (v ).max (), None ),
1133
- (mom .rolling_min , lambda v : Series (v ).min (), None ),
1134
- (mom .rolling_sum , lambda v : Series (v ).sum (), None ),
1135
- (mom .rolling_mean , lambda v : Series (v ).mean (), None ),
1136
- (mom .rolling_std , lambda v : Series (v ).std (), 1 ),
1137
- (mom .rolling_cov , lambda v : Series (v ).cov (Series (v )), None ),
1138
- (mom .rolling_corr , lambda v : Series (v ).corr (Series (v )), None ),
1139
- (mom .rolling_var , lambda v : Series (v ).var (), 1 ),
1140
- #(mom.rolling_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed
1141
- #(mom.rolling_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed
1142
- #(lambda x, window, min_periods, center: mom.rolling_quantile(x, window, 0.3, min_periods=min_periods, center=center),
1143
- # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed
1144
- (mom .rolling_median , lambda v : Series (v ).median (), None ),
1145
- (mom .rolling_max , np .nanmax , 1 ),
1146
- (mom .rolling_min , np .nanmin , 1 ),
1147
- (mom .rolling_sum , np .nansum , 1 ),
1148
- ]
1149
- if np .__version__ >= LooseVersion ('1.8.0' ):
1150
- base_functions += [
1151
- (mom .rolling_mean , np .nanmean , 1 ),
1152
- (mom .rolling_std , lambda v : np .nanstd (v , ddof = 1 ), 1 ),
1153
- (mom .rolling_var , lambda v : np .nanvar (v , ddof = 1 ), 1 ),
1154
- ]
1155
- if np .__version__ >= LooseVersion ('1.9.0' ):
1156
- base_functions += [
1157
- (mom .rolling_median , np .nanmedian , 1 ),
1158
- ]
1159
- no_nan_functions = [
1160
- (mom .rolling_max , np .max , None ),
1161
- (mom .rolling_min , np .min , None ),
1162
- (mom .rolling_sum , np .sum , None ),
1163
- (mom .rolling_mean , np .mean , None ),
1164
- (mom .rolling_std , lambda v : np .std (v , ddof = 1 ), 1 ),
1165
- (mom .rolling_var , lambda v : np .var (v , ddof = 1 ), 1 ),
1166
- (mom .rolling_median , np .median , None ),
1167
- ]
1168
-
1169
1136
for window in [1 , 2 , 3 , 10 , 20 ]:
1170
1137
for min_periods in set ([0 , 1 , 2 , 3 , 4 , window ]):
1171
1138
if min_periods and (min_periods > window ):
@@ -1195,11 +1162,14 @@ def test_rolling_consistency(self):
1195
1162
for (x , is_constant , no_nans ) in self .data :
1196
1163
1197
1164
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
1198
- functions = base_functions
1165
+ functions = self .base_functions
1166
+
1199
1167
# GH 8269
1200
1168
if no_nans :
1201
- functions = base_functions + no_nan_functions
1202
- for (rolling_f , f , require_min_periods ) in functions :
1169
+ functions = self .base_functions + self .no_nan_functions
1170
+ for (f , require_min_periods , name ) in functions :
1171
+ rolling_f = getattr (mom ,'rolling_{0}' .format (name ))
1172
+
1203
1173
if require_min_periods and (min_periods is not None ) and (min_periods < require_min_periods ):
1204
1174
continue
1205
1175
@@ -1214,7 +1184,8 @@ def test_rolling_consistency(self):
1214
1184
rolling_f_result = rolling_f (x , window = window , min_periods = min_periods , center = center )
1215
1185
rolling_apply_f_result = mom .rolling_apply (x , window = window , func = f ,
1216
1186
min_periods = min_periods , center = center )
1217
- assert_equal (rolling_f_result , rolling_apply_f_result )
1187
+ if not tm ._incompat_bottleneck_version (name ):
1188
+ assert_equal (rolling_f_result , rolling_apply_f_result )
1218
1189
1219
1190
if (rolling_f in [mom .rolling_cov , mom .rolling_corr ]) and isinstance (x , DataFrame ):
1220
1191
# test pairwise=True
0 commit comments