@@ -244,6 +244,12 @@ def roll_mean(x, window, min_periods=None, freq=None, center=False):
244
244
center = center )
245
245
self ._check_moment_func (roll_mean , np .mean )
246
246
247
+ # GH 8080
248
+ s = Series ([None , None , None ])
249
+ result = mom .rolling_apply (s , 2 , lambda x : len (x ), min_periods = 0 )
250
+ expected = Series ([1. , 2. , 2. ])
251
+ assert_series_equal (result , expected )
252
+
247
253
def test_rolling_apply_out_of_bounds (self ):
248
254
# #1850
249
255
arr = np .arange (4 )
@@ -814,6 +820,12 @@ def expanding_mean(x, min_periods=1, freq=None):
814
820
freq = freq )
815
821
self ._check_expanding (expanding_mean , np .mean )
816
822
823
+ # GH 8080
824
+ s = Series ([None , None , None ])
825
+ result = mom .expanding_apply (s , lambda x : len (x ), min_periods = 0 )
826
+ expected = Series ([1. , 2. , 3. ])
827
+ assert_series_equal (result , expected )
828
+
817
829
def test_expanding_apply_args_kwargs (self ):
818
830
def mean_w_arg (x , const ):
819
831
return np .mean (x ) + const
@@ -989,6 +1001,77 @@ def test_rolling_functions_window_non_shrinkage(self):
989
1001
df_result_panel = f (df )
990
1002
assert_panel_equal (df_result_panel , df_expected_panel )
991
1003
1004
+ def test_moment_functions_zero_length (self ):
1005
+ # GH 8056
1006
+ s = Series ()
1007
+ s_expected = s
1008
+ df1 = DataFrame ()
1009
+ df1_expected = df1
1010
+ df1_expected_panel = Panel (items = df1 .index , major_axis = df1 .columns , minor_axis = df1 .columns )
1011
+ df2 = DataFrame (columns = ['a' ])
1012
+ df2_expected = df2
1013
+ df2_expected_panel = Panel (items = df2 .index , major_axis = df2 .columns , minor_axis = df2 .columns )
1014
+
1015
+ functions = [lambda x : mom .expanding_count (x ),
1016
+ lambda x : mom .expanding_cov (x , x , pairwise = False , min_periods = 5 ),
1017
+ lambda x : mom .expanding_corr (x , x , pairwise = False , min_periods = 5 ),
1018
+ lambda x : mom .expanding_max (x , min_periods = 5 ),
1019
+ lambda x : mom .expanding_min (x , min_periods = 5 ),
1020
+ lambda x : mom .expanding_sum (x , min_periods = 5 ),
1021
+ lambda x : mom .expanding_mean (x , min_periods = 5 ),
1022
+ lambda x : mom .expanding_std (x , min_periods = 5 ),
1023
+ lambda x : mom .expanding_var (x , min_periods = 5 ),
1024
+ lambda x : mom .expanding_skew (x , min_periods = 5 ),
1025
+ lambda x : mom .expanding_kurt (x , min_periods = 5 ),
1026
+ lambda x : mom .expanding_quantile (x , quantile = 0.5 , min_periods = 5 ),
1027
+ lambda x : mom .expanding_median (x , min_periods = 5 ),
1028
+ lambda x : mom .expanding_apply (x , func = sum , min_periods = 5 ),
1029
+ lambda x : mom .rolling_count (x , window = 10 ),
1030
+ lambda x : mom .rolling_cov (x , x , pairwise = False , window = 10 , min_periods = 5 ),
1031
+ lambda x : mom .rolling_corr (x , x , pairwise = False , window = 10 , min_periods = 5 ),
1032
+ lambda x : mom .rolling_max (x , window = 10 , min_periods = 5 ),
1033
+ lambda x : mom .rolling_min (x , window = 10 , min_periods = 5 ),
1034
+ lambda x : mom .rolling_sum (x , window = 10 , min_periods = 5 ),
1035
+ lambda x : mom .rolling_mean (x , window = 10 , min_periods = 5 ),
1036
+ lambda x : mom .rolling_std (x , window = 10 , min_periods = 5 ),
1037
+ lambda x : mom .rolling_var (x , window = 10 , min_periods = 5 ),
1038
+ lambda x : mom .rolling_skew (x , window = 10 , min_periods = 5 ),
1039
+ lambda x : mom .rolling_kurt (x , window = 10 , min_periods = 5 ),
1040
+ lambda x : mom .rolling_quantile (x , quantile = 0.5 , window = 10 , min_periods = 5 ),
1041
+ lambda x : mom .rolling_median (x , window = 10 , min_periods = 5 ),
1042
+ lambda x : mom .rolling_apply (x , func = sum , window = 10 , min_periods = 5 ),
1043
+ lambda x : mom .rolling_window (x , win_type = 'boxcar' , window = 10 , min_periods = 5 ),
1044
+ ]
1045
+ for f in functions :
1046
+ try :
1047
+ s_result = f (s )
1048
+ assert_series_equal (s_result , s_expected )
1049
+
1050
+ df1_result = f (df1 )
1051
+ assert_frame_equal (df1_result , df1_expected )
1052
+
1053
+ df2_result = f (df2 )
1054
+ assert_frame_equal (df2_result , df2_expected )
1055
+ except (ImportError ):
1056
+
1057
+ # scipy needed for rolling_window
1058
+ continue
1059
+
1060
+ functions = [lambda x : mom .expanding_cov (x , x , pairwise = True , min_periods = 5 ),
1061
+ lambda x : mom .expanding_corr (x , x , pairwise = True , min_periods = 5 ),
1062
+ lambda x : mom .rolling_cov (x , x , pairwise = True , window = 10 , min_periods = 5 ),
1063
+ lambda x : mom .rolling_corr (x , x , pairwise = True , window = 10 , min_periods = 5 ),
1064
+ # rolling_corr_pairwise is depracated, so the following line should be deleted
1065
+ # when rolling_corr_pairwise is removed.
1066
+ lambda x : mom .rolling_corr_pairwise (x , x , window = 10 , min_periods = 5 ),
1067
+ ]
1068
+ for f in functions :
1069
+ df1_result_panel = f (df1 )
1070
+ assert_panel_equal (df1_result_panel , df1_expected_panel )
1071
+
1072
+ df2_result_panel = f (df2 )
1073
+ assert_panel_equal (df2_result_panel , df2_expected_panel )
1074
+
992
1075
def test_expanding_cov_pairwise_diff_length (self ):
993
1076
# GH 7512
994
1077
df1 = DataFrame ([[1 ,5 ], [3 , 2 ], [3 ,9 ]], columns = ['A' ,'B' ])
0 commit comments