@@ -372,7 +372,10 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None, center=False,
372372 y : type of input
373373 """
374374 arg = _conv_timerule (arg , freq , how )
375- calc = lambda x : func (x , window , minp = minp , args = args , kwargs = kwargs ,
375+ offset = int ((window - 1 ) / 2. ) if center else 0
376+ additional_nans = np .array ([np .NaN ] * offset )
377+ calc = lambda x : func (np .concatenate ((x , additional_nans )) if center else x ,
378+ window , minp = minp , args = args , kwargs = kwargs ,
376379 ** kwds )
377380 return_hook , values = _process_data_structure (arg )
378381 # actually calculate the moment. Faster way to do this?
@@ -381,10 +384,10 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None, center=False,
381384 else :
382385 result = calc (values )
383386
384- rs = return_hook (result )
385387 if center :
386- rs = _center_window (rs , window , axis )
387- return rs
388+ result = _center_window (result , window , axis )
389+
390+ return return_hook (result )
388391
389392
390393def _center_window (rs , window , axis ):
@@ -393,20 +396,13 @@ def _center_window(rs, window, axis):
393396 "dimensions" )
394397
395398 offset = int ((window - 1 ) / 2. )
396- if isinstance (rs , (Series , DataFrame , Panel )):
397- rs = rs .shift (- offset , axis = axis )
398- else :
399- rs_indexer = [slice (None )] * rs .ndim
400- rs_indexer [axis ] = slice (None , - offset )
401-
402- lead_indexer = [slice (None )] * rs .ndim
403- lead_indexer [axis ] = slice (offset , None )
404-
405- na_indexer = [slice (None )] * rs .ndim
406- na_indexer [axis ] = slice (- offset , None )
407-
408- rs [tuple (rs_indexer )] = np .copy (rs [tuple (lead_indexer )])
409- rs [tuple (na_indexer )] = np .nan
399+ if offset > 0 :
400+ if isinstance (rs , (Series , DataFrame , Panel )):
401+ rs = rs .slice_shift (- offset , axis = axis )
402+ else :
403+ lead_indexer = [slice (None )] * rs .ndim
404+ lead_indexer [axis ] = slice (offset , None )
405+ rs = np .copy (rs [tuple (lead_indexer )])
410406 return rs
411407
412408
@@ -821,13 +817,16 @@ def rolling_window(arg, window=None, win_type=None, min_periods=None,
821817 arg = _conv_timerule (arg , freq , how )
822818 return_hook , values = _process_data_structure (arg )
823819
824- f = lambda x : algos .roll_window (x , window , minp , avg = mean )
820+ offset = int ((len (window ) - 1 ) / 2. ) if center else 0
821+ additional_nans = np .array ([np .NaN ] * offset )
822+ f = lambda x : algos .roll_window (np .concatenate ((x , additional_nans )) if center else x ,
823+ window , minp , avg = mean )
825824 result = np .apply_along_axis (f , axis , values )
826825
827- rs = return_hook (result )
828826 if center :
829- rs = _center_window (rs , len (window ), axis )
830- return rs
827+ result = _center_window (result , len (window ), axis )
828+
829+ return return_hook (result )
831830
832831
833832def _validate_win_type (win_type , kwargs ):
@@ -856,14 +855,14 @@ def _expanding_func(func, desc, check_minp=_use_window):
856855 @Substitution (desc , _unary_arg , _expanding_kw , _type_of_input_retval , "" )
857856 @Appender (_doc_template )
858857 @wraps (func )
859- def f (arg , min_periods = 1 , freq = None , center = False , ** kwargs ):
858+ def f (arg , min_periods = 1 , freq = None , ** kwargs ):
860859 window = len (arg )
861860
862861 def call_cython (arg , window , minp , args = (), kwargs = {}, ** kwds ):
863862 minp = check_minp (minp , window )
864863 return func (arg , window , minp , ** kwds )
865864 return _rolling_moment (arg , window , call_cython , min_periods , freq = freq ,
866- center = center , ** kwargs )
865+ ** kwargs )
867866
868867 return f
869868
@@ -887,7 +886,7 @@ def call_cython(arg, window, minp, args=(), kwargs={}, **kwds):
887886 check_minp = _require_min_periods (4 ))
888887
889888
890- def expanding_count (arg , freq = None , center = False ):
889+ def expanding_count (arg , freq = None ):
891890 """
892891 Expanding count of number of non-NaN observations.
893892
@@ -897,8 +896,6 @@ def expanding_count(arg, freq=None, center=False):
897896 freq : string or DateOffset object, optional (default None)
898897 Frequency to conform the data to before computing the statistic. Specified
899898 as a frequency string or DateOffset object.
900- center : boolean, default False
901- Whether the label should correspond with center of window.
902899
903900 Returns
904901 -------
@@ -910,11 +907,10 @@ def expanding_count(arg, freq=None, center=False):
910907 frequency by resampling the data. This is done with the default parameters
911908 of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
912909 """
913- return rolling_count (arg , len (arg ), freq = freq , center = center )
910+ return rolling_count (arg , len (arg ), freq = freq )
914911
915912
916- def expanding_quantile (arg , quantile , min_periods = 1 , freq = None ,
917- center = False ):
913+ def expanding_quantile (arg , quantile , min_periods = 1 , freq = None ):
918914 """Expanding quantile.
919915
920916 Parameters
@@ -928,8 +924,6 @@ def expanding_quantile(arg, quantile, min_periods=1, freq=None,
928924 freq : string or DateOffset object, optional (default None)
929925 Frequency to conform the data to before computing the statistic. Specified
930926 as a frequency string or DateOffset object.
931- center : boolean, default False
932- Whether the label should correspond with center of window.
933927
934928 Returns
935929 -------
@@ -942,14 +936,13 @@ def expanding_quantile(arg, quantile, min_periods=1, freq=None,
942936 of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
943937 """
944938 return rolling_quantile (arg , len (arg ), quantile , min_periods = min_periods ,
945- freq = freq , center = center )
939+ freq = freq )
946940
947941
948942@Substitution ("Unbiased expanding covariance." , _binary_arg_flex ,
949943 _expanding_kw + _pairwise_kw , _flex_retval , "" )
950944@Appender (_doc_template )
951- def expanding_cov (arg1 , arg2 = None , min_periods = 1 , freq = None , center = False ,
952- pairwise = None ):
945+ def expanding_cov (arg1 , arg2 = None , min_periods = 1 , freq = None , pairwise = None ):
953946 if arg2 is None :
954947 arg2 = arg1
955948 pairwise = True if pairwise is None else pairwise
@@ -960,14 +953,13 @@ def expanding_cov(arg1, arg2=None, min_periods=1, freq=None, center=False,
960953 window = len (arg1 ) + len (arg2 )
961954 return rolling_cov (arg1 , arg2 , window ,
962955 min_periods = min_periods , freq = freq ,
963- center = center , pairwise = pairwise )
956+ pairwise = pairwise )
964957
965958
966959@Substitution ("Expanding sample correlation." , _binary_arg_flex ,
967960 _expanding_kw + _pairwise_kw , _flex_retval , "" )
968961@Appender (_doc_template )
969- def expanding_corr (arg1 , arg2 = None , min_periods = 1 , freq = None , center = False ,
970- pairwise = None ):
962+ def expanding_corr (arg1 , arg2 = None , min_periods = 1 , freq = None , pairwise = None ):
971963 if arg2 is None :
972964 arg2 = arg1
973965 pairwise = True if pairwise is None else pairwise
@@ -978,22 +970,21 @@ def expanding_corr(arg1, arg2=None, min_periods=1, freq=None, center=False,
978970 window = len (arg1 ) + len (arg2 )
979971 return rolling_corr (arg1 , arg2 , window ,
980972 min_periods = min_periods ,
981- freq = freq , center = center , pairwise = pairwise )
973+ freq = freq , pairwise = pairwise )
982974
983975
984976@Substitution ("Deprecated. Use expanding_corr(..., pairwise=True) instead.\n \n "
985977 "Pairwise expanding sample correlation" , _pairwise_arg ,
986978 _expanding_kw , _pairwise_retval , "" )
987979@Appender (_doc_template )
988- def expanding_corr_pairwise (df1 , df2 = None , min_periods = 1 , freq = None ,
989- center = False ):
980+ def expanding_corr_pairwise (df1 , df2 = None , min_periods = 1 , freq = None ):
990981 import warnings
991982 warnings .warn ("expanding_corr_pairwise is deprecated, use expanding_corr(..., pairwise=True)" , FutureWarning )
992983 return expanding_corr (df1 , df2 , min_periods = min_periods ,
993- freq = freq , center = center , pairwise = True )
984+ freq = freq , pairwise = True )
994985
995986
996- def expanding_apply (arg , func , min_periods = 1 , freq = None , center = False ,
987+ def expanding_apply (arg , func , min_periods = 1 , freq = None ,
997988 args = (), kwargs = {}):
998989 """Generic expanding function application.
999990
@@ -1008,8 +999,6 @@ def expanding_apply(arg, func, min_periods=1, freq=None, center=False,
1008999 freq : string or DateOffset object, optional (default None)
10091000 Frequency to conform the data to before computing the statistic. Specified
10101001 as a frequency string or DateOffset object.
1011- center : boolean, default False
1012- Whether the label should correspond with center of window.
10131002 args : tuple
10141003 Passed on to func
10151004 kwargs : dict
@@ -1027,4 +1016,4 @@ def expanding_apply(arg, func, min_periods=1, freq=None, center=False,
10271016 """
10281017 window = len (arg )
10291018 return rolling_apply (arg , window , func , min_periods = min_periods , freq = freq ,
1030- center = center , args = args , kwargs = kwargs )
1019+ args = args , kwargs = kwargs )
0 commit comments