@@ -372,7 +372,10 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None, center=False,
372
372
y : type of input
373
373
"""
374
374
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 ,
376
379
** kwds )
377
380
return_hook , values = _process_data_structure (arg )
378
381
# 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,
381
384
else :
382
385
result = calc (values )
383
386
384
- rs = return_hook (result )
385
387
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 )
388
391
389
392
390
393
def _center_window (rs , window , axis ):
@@ -393,20 +396,13 @@ def _center_window(rs, window, axis):
393
396
"dimensions" )
394
397
395
398
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 )])
410
406
return rs
411
407
412
408
@@ -821,13 +817,16 @@ def rolling_window(arg, window=None, win_type=None, min_periods=None,
821
817
arg = _conv_timerule (arg , freq , how )
822
818
return_hook , values = _process_data_structure (arg )
823
819
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 )
825
824
result = np .apply_along_axis (f , axis , values )
826
825
827
- rs = return_hook (result )
828
826
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 )
831
830
832
831
833
832
def _validate_win_type (win_type , kwargs ):
@@ -856,14 +855,14 @@ def _expanding_func(func, desc, check_minp=_use_window):
856
855
@Substitution (desc , _unary_arg , _expanding_kw , _type_of_input_retval , "" )
857
856
@Appender (_doc_template )
858
857
@wraps (func )
859
- def f (arg , min_periods = 1 , freq = None , center = False , ** kwargs ):
858
+ def f (arg , min_periods = 1 , freq = None , ** kwargs ):
860
859
window = len (arg )
861
860
862
861
def call_cython (arg , window , minp , args = (), kwargs = {}, ** kwds ):
863
862
minp = check_minp (minp , window )
864
863
return func (arg , window , minp , ** kwds )
865
864
return _rolling_moment (arg , window , call_cython , min_periods , freq = freq ,
866
- center = center , ** kwargs )
865
+ ** kwargs )
867
866
868
867
return f
869
868
@@ -887,7 +886,7 @@ def call_cython(arg, window, minp, args=(), kwargs={}, **kwds):
887
886
check_minp = _require_min_periods (4 ))
888
887
889
888
890
- def expanding_count (arg , freq = None , center = False ):
889
+ def expanding_count (arg , freq = None ):
891
890
"""
892
891
Expanding count of number of non-NaN observations.
893
892
@@ -897,8 +896,6 @@ def expanding_count(arg, freq=None, center=False):
897
896
freq : string or DateOffset object, optional (default None)
898
897
Frequency to conform the data to before computing the statistic. Specified
899
898
as a frequency string or DateOffset object.
900
- center : boolean, default False
901
- Whether the label should correspond with center of window.
902
899
903
900
Returns
904
901
-------
@@ -910,11 +907,10 @@ def expanding_count(arg, freq=None, center=False):
910
907
frequency by resampling the data. This is done with the default parameters
911
908
of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
912
909
"""
913
- return rolling_count (arg , len (arg ), freq = freq , center = center )
910
+ return rolling_count (arg , len (arg ), freq = freq )
914
911
915
912
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 ):
918
914
"""Expanding quantile.
919
915
920
916
Parameters
@@ -928,8 +924,6 @@ def expanding_quantile(arg, quantile, min_periods=1, freq=None,
928
924
freq : string or DateOffset object, optional (default None)
929
925
Frequency to conform the data to before computing the statistic. Specified
930
926
as a frequency string or DateOffset object.
931
- center : boolean, default False
932
- Whether the label should correspond with center of window.
933
927
934
928
Returns
935
929
-------
@@ -942,14 +936,13 @@ def expanding_quantile(arg, quantile, min_periods=1, freq=None,
942
936
of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
943
937
"""
944
938
return rolling_quantile (arg , len (arg ), quantile , min_periods = min_periods ,
945
- freq = freq , center = center )
939
+ freq = freq )
946
940
947
941
948
942
@Substitution ("Unbiased expanding covariance." , _binary_arg_flex ,
949
943
_expanding_kw + _pairwise_kw , _flex_retval , "" )
950
944
@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 ):
953
946
if arg2 is None :
954
947
arg2 = arg1
955
948
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,
960
953
window = len (arg1 ) + len (arg2 )
961
954
return rolling_cov (arg1 , arg2 , window ,
962
955
min_periods = min_periods , freq = freq ,
963
- center = center , pairwise = pairwise )
956
+ pairwise = pairwise )
964
957
965
958
966
959
@Substitution ("Expanding sample correlation." , _binary_arg_flex ,
967
960
_expanding_kw + _pairwise_kw , _flex_retval , "" )
968
961
@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 ):
971
963
if arg2 is None :
972
964
arg2 = arg1
973
965
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,
978
970
window = len (arg1 ) + len (arg2 )
979
971
return rolling_corr (arg1 , arg2 , window ,
980
972
min_periods = min_periods ,
981
- freq = freq , center = center , pairwise = pairwise )
973
+ freq = freq , pairwise = pairwise )
982
974
983
975
984
976
@Substitution ("Deprecated. Use expanding_corr(..., pairwise=True) instead.\n \n "
985
977
"Pairwise expanding sample correlation" , _pairwise_arg ,
986
978
_expanding_kw , _pairwise_retval , "" )
987
979
@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 ):
990
981
import warnings
991
982
warnings .warn ("expanding_corr_pairwise is deprecated, use expanding_corr(..., pairwise=True)" , FutureWarning )
992
983
return expanding_corr (df1 , df2 , min_periods = min_periods ,
993
- freq = freq , center = center , pairwise = True )
984
+ freq = freq , pairwise = True )
994
985
995
986
996
- def expanding_apply (arg , func , min_periods = 1 , freq = None , center = False ,
987
+ def expanding_apply (arg , func , min_periods = 1 , freq = None ,
997
988
args = (), kwargs = {}):
998
989
"""Generic expanding function application.
999
990
@@ -1008,8 +999,6 @@ def expanding_apply(arg, func, min_periods=1, freq=None, center=False,
1008
999
freq : string or DateOffset object, optional (default None)
1009
1000
Frequency to conform the data to before computing the statistic. Specified
1010
1001
as a frequency string or DateOffset object.
1011
- center : boolean, default False
1012
- Whether the label should correspond with center of window.
1013
1002
args : tuple
1014
1003
Passed on to func
1015
1004
kwargs : dict
@@ -1027,4 +1016,4 @@ def expanding_apply(arg, func, min_periods=1, freq=None, center=False,
1027
1016
"""
1028
1017
window = len (arg )
1029
1018
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