5
5
from datetime import datetime
6
6
from numpy .random import randn
7
7
import numpy as np
8
+ from distutils .version import LooseVersion
8
9
9
10
from pandas import Series , DataFrame , Panel , bdate_range , isnull , notnull
10
11
from pandas .util .testing import (
@@ -877,6 +878,45 @@ def _ewma(s, com, min_periods, adjust, ignore_na):
877
878
var_debiasing_factors = lambda x : _variance_debiasing_factors (x , com = com , adjust = adjust , ignore_na = ignore_na ))
878
879
879
880
def test_expanding_consistency (self ):
881
+ base_functions = [
882
+ (mom .expanding_count , lambda v : Series (v ).count (), None ),
883
+ (mom .expanding_max , lambda v : Series (v ).max (), None ),
884
+ (mom .expanding_min , lambda v : Series (v ).min (), None ),
885
+ (mom .expanding_sum , lambda v : Series (v ).sum (), None ),
886
+ (mom .expanding_mean , lambda v : Series (v ).mean (), None ),
887
+ (mom .expanding_std , lambda v : Series (v ).std (), 1 ),
888
+ (mom .expanding_cov , lambda v : Series (v ).cov (Series (v )), None ),
889
+ (mom .expanding_corr , lambda v : Series (v ).corr (Series (v )), None ),
890
+ (mom .expanding_var , lambda v : Series (v ).var (), 1 ),
891
+ #(mom.expanding_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed
892
+ #(mom.expanding_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed
893
+ #(lambda x, min_periods: mom.expanding_quantile(x, 0.3, min_periods=min_periods),
894
+ # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed
895
+ (mom .expanding_median , lambda v : Series (v ).median (), None ),
896
+ (mom .expanding_max , np .nanmax , 1 ),
897
+ (mom .expanding_min , np .nanmin , 1 ),
898
+ (mom .expanding_sum , np .nansum , 1 ),
899
+ ]
900
+ if np .__version__ >= LooseVersion ('1.8.0' ):
901
+ base_functions += [
902
+ (mom .expanding_mean , np .nanmean , 1 ),
903
+ (mom .expanding_std , lambda v : np .nanstd (v , ddof = 1 ), 1 ),
904
+ (mom .expanding_var , lambda v : np .nanvar (v , ddof = 1 ), 1 ),
905
+ ]
906
+ if np .__version__ >= LooseVersion ('1.9.0' ):
907
+ base_functions += [
908
+ (mom .expanding_median , np .nanmedian , 1 ),
909
+ ]
910
+ no_nan_functions = [
911
+ (mom .expanding_max , np .max , None ),
912
+ (mom .expanding_min , np .min , None ),
913
+ (mom .expanding_sum , np .sum , None ),
914
+ (mom .expanding_mean , np .mean , None ),
915
+ (mom .expanding_std , lambda v : np .std (v , ddof = 1 ), 1 ),
916
+ (mom .expanding_var , lambda v : np .var (v , ddof = 1 ), 1 ),
917
+ (mom .expanding_median , np .median , None ),
918
+ ]
919
+
880
920
for min_periods in [0 , 1 , 2 , 3 , 4 ]:
881
921
882
922
# test consistency between different expanding_* moments
@@ -895,25 +935,15 @@ def test_expanding_consistency(self):
895
935
var_debiasing_factors = lambda x : mom .expanding_count (x ) / (mom .expanding_count (x ) - 1. ).replace (0. , np .nan )
896
936
)
897
937
898
- # test consistency between expanding_xyz() and expanding_apply of Series/DataFrame.xyz()
938
+ # test consistency between expanding_xyz() and either (a) expanding_apply of Series.xyz(),
939
+ # or (b) expanding_apply of np.nanxyz()
899
940
for x in self ._test_data ():
900
941
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
901
- for (expanding_f , f , require_min_periods ) in [
902
- (mom .expanding_count , lambda v : Series (v ).count (), None ),
903
- (mom .expanding_max , lambda v : Series (v ).max (), None ),
904
- (mom .expanding_min , lambda v : Series (v ).min (), None ),
905
- (mom .expanding_sum , lambda v : Series (v ).sum (), None ),
906
- (mom .expanding_mean , lambda v : Series (v ).mean (), None ),
907
- (mom .expanding_std , lambda v : Series (v ).std (), 1 ),
908
- (mom .expanding_cov , lambda v : Series (v ).cov (Series (v )), None ),
909
- (mom .expanding_corr , lambda v : Series (v ).corr (Series (v )), None ),
910
- (mom .expanding_var , lambda v : Series (v ).var (), 1 ),
911
- #(mom.expanding_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed
912
- #(mom.expanding_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed
913
- #(lambda x, min_periods: mom.expanding_quantile(x, 0.3, min_periods=min_periods),
914
- # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed
915
- (mom .expanding_median , lambda v : Series (v ).median (), None ),
916
- ]:
942
+ functions = base_functions
943
+ # GH 8269
944
+ if x .notnull ().all ().all ():
945
+ functions = base_functions + no_nan_functions
946
+ for (expanding_f , f , require_min_periods ) in functions :
917
947
if require_min_periods and (min_periods is not None ) and (min_periods < require_min_periods ):
918
948
continue
919
949
@@ -938,7 +968,46 @@ def test_expanding_consistency(self):
938
968
assert_panel_equal (expanding_f_result , expected )
939
969
940
970
def test_rolling_consistency (self ):
941
- for window in [1 , 3 , 10 , 20 ]:
971
+ base_functions = [
972
+ (mom .rolling_count , lambda v : Series (v ).count (), None ),
973
+ (mom .rolling_max , lambda v : Series (v ).max (), None ),
974
+ (mom .rolling_min , lambda v : Series (v ).min (), None ),
975
+ (mom .rolling_sum , lambda v : Series (v ).sum (), None ),
976
+ (mom .rolling_mean , lambda v : Series (v ).mean (), None ),
977
+ (mom .rolling_std , lambda v : Series (v ).std (), 1 ),
978
+ (mom .rolling_cov , lambda v : Series (v ).cov (Series (v )), None ),
979
+ (mom .rolling_corr , lambda v : Series (v ).corr (Series (v )), None ),
980
+ (mom .rolling_var , lambda v : Series (v ).var (), 1 ),
981
+ #(mom.rolling_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed
982
+ # (mom.rolling_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed
983
+ #(lambda x, window, min_periods, center: mom.rolling_quantile(x, window, 0.3, min_periods=min_periods, center=center),
984
+ # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed
985
+ (mom .rolling_median , lambda v : Series (v ).median (), None ),
986
+ (mom .rolling_max , np .nanmax , 1 ),
987
+ (mom .rolling_min , np .nanmin , 1 ),
988
+ (mom .rolling_sum , np .nansum , 1 ),
989
+ ]
990
+ if np .__version__ >= LooseVersion ('1.8.0' ):
991
+ base_functions += [
992
+ (mom .rolling_mean , np .nanmean , 1 ),
993
+ (mom .rolling_std , lambda v : np .nanstd (v , ddof = 1 ), 1 ),
994
+ (mom .rolling_var , lambda v : np .nanvar (v , ddof = 1 ), 1 ),
995
+ ]
996
+ if np .__version__ >= LooseVersion ('1.9.0' ):
997
+ base_functions += [
998
+ (mom .rolling_median , np .nanmedian , 1 ),
999
+ ]
1000
+ no_nan_functions = [
1001
+ (mom .rolling_max , np .max , None ),
1002
+ (mom .rolling_min , np .min , None ),
1003
+ (mom .rolling_sum , np .sum , None ),
1004
+ (mom .rolling_mean , np .mean , None ),
1005
+ (mom .rolling_std , lambda v : np .std (v , ddof = 1 ), 1 ),
1006
+ (mom .rolling_var , lambda v : np .var (v , ddof = 1 ), 1 ),
1007
+ (mom .rolling_median , np .median , None ),
1008
+ ]
1009
+
1010
+ for window in [1 , 2 , 3 , 10 , 20 ]:
942
1011
for min_periods in set ([0 , 1 , 2 , 3 , 4 , window ]):
943
1012
if min_periods and (min_periods > window ):
944
1013
continue
@@ -962,25 +1031,15 @@ def test_rolling_consistency(self):
962
1031
(mom .rolling_count (x , window = window , center = center ) - 1. ).replace (0. , np .nan )),
963
1032
)
964
1033
965
- # test consistency between rolling_xyz and rolling_apply of Series/DataFrame.xyz
1034
+ # test consistency between rolling_xyz() and either (a) rolling_apply of Series.xyz(),
1035
+ # or (b) rolling_apply of np.nanxyz()
966
1036
for x in self ._test_data ():
967
1037
assert_equal = assert_series_equal if isinstance (x , Series ) else assert_frame_equal
968
- for (rolling_f , f , require_min_periods ) in [
969
- (mom .rolling_count , lambda v : Series (v ).count (), None ),
970
- (mom .rolling_max , lambda v : Series (v ).max (), None ),
971
- (mom .rolling_min , lambda v : Series (v ).min (), None ),
972
- (mom .rolling_sum , lambda v : Series (v ).sum (), None ),
973
- (mom .rolling_mean , lambda v : Series (v ).mean (), None ),
974
- (mom .rolling_std , lambda v : Series (v ).std (), 1 ),
975
- (mom .rolling_cov , lambda v : Series (v ).cov (Series (v )), None ),
976
- (mom .rolling_corr , lambda v : Series (v ).corr (Series (v )), None ),
977
- (mom .rolling_var , lambda v : Series (v ).var (), 1 ),
978
- #(mom.rolling_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed
979
- # (mom.rolling_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed
980
- #(lambda x, window, min_periods, center: mom.rolling_quantile(x, window, 0.3, min_periods=min_periods, center=center),
981
- # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed
982
- (mom .rolling_median , lambda v : Series (v ).median (), None ),
983
- ]:
1038
+ functions = base_functions
1039
+ # GH 8269
1040
+ if x .notnull ().all ().all ():
1041
+ functions = base_functions + no_nan_functions
1042
+ for (rolling_f , f , require_min_periods ) in functions :
984
1043
if require_min_periods and (min_periods is not None ) and (min_periods < require_min_periods ):
985
1044
continue
986
1045
0 commit comments