@@ -120,7 +120,7 @@ def df_rolling_method_other_df_codegen(method_name, self, other, args=None, kws=
120120 ' else:' ,
121121 ' _pairwise = pairwise' ,
122122 ' if _pairwise:' ,
123- ' raise ValueError("Method rolling.corr (). The object pairwise\\ n expected: False, None")'
123+ f ' raise ValueError("Method rolling.{ method_name } (). The object pairwise\\ n expected: False, None")'
124124 ]
125125
126126 data_length = 'len(get_dataframe_data(self._data, 0))' if data_columns else '0'
@@ -139,7 +139,7 @@ def df_rolling_method_other_df_codegen(method_name, self, other, args=None, kws=
139139 f' series_{ col } = pandas.Series(data_{ col } )' ,
140140 f' { other_series } = pandas.Series(other_data_{ col } )' ,
141141 f' rolling_{ col } = series_{ col } .rolling({ rolling_params } )' ,
142- f' result_{ col } = rolling_{ col } .corr ({ method_params } )' ,
142+ f' result_{ col } = rolling_{ col } .{ method_name } ({ method_params } )' ,
143143 f' { res_data } = result_{ col } ._data[:length]'
144144 ]
145145 else :
@@ -182,32 +182,41 @@ def df_rolling_method_main_codegen(method_params, df_columns, method_name):
182182 return func_lines
183183
184184
185- def df_rolling_method_other_none_codegen (method_name , self , args = None , kws = None ):
186- args = args or []
187- kwargs = kws or {}
185+ def gen_df_rolling_method_other_none_codegen (rewrite_name = None ):
186+ """Generate df.rolling method code generator based on name of the method"""
187+ def df_rolling_method_other_none_codegen (method_name , self , args = None , kws = None ):
188+ _method_name = rewrite_name or method_name
189+ args = args or []
190+ kwargs = kws or {}
188191
189- impl_params = ['self' ] + args + params2list (kwargs )
190- impl_params_as_str = ', ' .join (impl_params )
192+ impl_params = ['self' ] + args + params2list (kwargs )
193+ impl_params_as_str = ', ' .join (impl_params )
191194
192- impl_name = f'_df_rolling_{ method_name } _other_none_impl'
193- func_lines = [f'def { impl_name } ({ impl_params_as_str } ):' ]
195+ impl_name = f'_df_rolling_{ _method_name } _other_none_impl'
196+ func_lines = [f'def { impl_name } ({ impl_params_as_str } ):' ]
194197
195- if 'pairwise' in kwargs :
196- func_lines += [
197- ' if pairwise is None:' ,
198- ' _pairwise = True' ,
199- ' else:' ,
200- ' _pairwise = pairwise' ,
201- ' if _pairwise:' ,
202- ' raise ValueError("Method rolling.corr (). The object pairwise\\ n expected: False")'
203- ]
204- method_params = args + ['{}={}' .format (k , k ) for k in kwargs if k != 'other' ]
205- func_lines += df_rolling_method_main_codegen (method_params , self .data .columns , method_name )
206- func_text = '\n ' .join (func_lines )
198+ if 'pairwise' in kwargs :
199+ func_lines += [
200+ ' if pairwise is None:' ,
201+ ' _pairwise = True' ,
202+ ' else:' ,
203+ ' _pairwise = pairwise' ,
204+ ' if _pairwise:' ,
205+ f ' raise ValueError("Method rolling.{ _method_name } (). The object pairwise\\ n expected: False")'
206+ ]
207+ method_params = args + ['{}={}' .format (k , k ) for k in kwargs if k != 'other' ]
208+ func_lines += df_rolling_method_main_codegen (method_params , self .data .columns , method_name )
209+ func_text = '\n ' .join (func_lines )
207210
208- global_vars = {'pandas' : pandas , 'get_dataframe_data' : get_dataframe_data }
211+ global_vars = {'pandas' : pandas , 'get_dataframe_data' : get_dataframe_data }
209212
210- return func_text , global_vars
213+ return func_text , global_vars
214+
215+ return df_rolling_method_other_none_codegen
216+
217+
218+ df_rolling_method_other_none_codegen = gen_df_rolling_method_other_none_codegen ()
219+ df_rolling_cov_other_none_codegen = gen_df_rolling_method_other_none_codegen ('cov' )
211220
212221
213222def df_rolling_method_codegen (method_name , self , args = None , kws = None ):
@@ -249,6 +258,16 @@ def gen_df_rolling_method_other_none_impl(method_name, self, args=None, kws=None
249258 return _impl
250259
251260
261+ def gen_df_rolling_cov_other_none_impl (method_name , self , args = None , kws = None ):
262+ func_text , global_vars = df_rolling_cov_other_none_codegen (method_name , self ,
263+ args = args , kws = kws )
264+ loc_vars = {}
265+ exec (func_text , global_vars , loc_vars )
266+ _impl = loc_vars [f'_df_rolling_cov_other_none_impl' ]
267+
268+ return _impl
269+
270+
252271def gen_df_rolling_method_impl (method_name , self , args = None , kws = None ):
253272 func_text , global_vars = df_rolling_method_codegen (method_name , self ,
254273 args = args , kws = kws )
@@ -308,6 +327,37 @@ def sdc_pandas_dataframe_rolling_count(self):
308327 return gen_df_rolling_method_impl ('count' , self )
309328
310329
330+ @sdc_overload_method (DataFrameRollingType , 'cov' )
331+ def sdc_pandas_dataframe_rolling_cov (self , other = None , pairwise = None , ddof = 1 ):
332+
333+ ty_checker = TypeChecker ('Method rolling.cov().' )
334+ ty_checker .check (self , DataFrameRollingType )
335+
336+ accepted_other = (Omitted , NoneType , DataFrameType , SeriesType )
337+ if not isinstance (other , accepted_other ) and other is not None :
338+ ty_checker .raise_exc (other , 'DataFrame, Series' , 'other' )
339+
340+ accepted_pairwise = (bool , Boolean , Omitted , NoneType )
341+ if not isinstance (pairwise , accepted_pairwise ) and pairwise is not None :
342+ ty_checker .raise_exc (pairwise , 'bool' , 'pairwise' )
343+
344+ if not isinstance (ddof , (int , Integer , Omitted )):
345+ ty_checker .raise_exc (ddof , 'int' , 'ddof' )
346+
347+ none_other = isinstance (other , (Omitted , NoneType )) or other is None
348+ kws = {'other' : 'None' , 'pairwise' : 'None' , 'ddof' : '1' }
349+
350+ if none_other :
351+ # method _df_cov in comparison to method cov doesn't align input data
352+ # by replacing infinite and matched finite values with nans
353+ return gen_df_rolling_cov_other_none_impl ('_df_cov' , self , kws = kws )
354+
355+ if isinstance (other , DataFrameType ):
356+ return gen_df_rolling_method_other_df_impl ('cov' , self , other , kws = kws )
357+
358+ return gen_df_rolling_method_impl ('cov' , self , kws = kws )
359+
360+
311361@sdc_overload_method (DataFrameRollingType , 'kurt' )
312362def sdc_pandas_dataframe_rolling_kurt (self ):
313363
@@ -457,6 +507,28 @@ def sdc_pandas_dataframe_rolling_var(self, ddof=1):
457507 'extra_params' : ''
458508})
459509
510+ sdc_pandas_dataframe_rolling_cov .__doc__ = sdc_pandas_dataframe_rolling_docstring_tmpl .format (** {
511+ 'method_name' : 'cov' ,
512+ 'example_caption' : 'Calculate rolling covariance.' ,
513+ 'limitations_block' :
514+ """
515+ Limitations
516+ -----------
517+ DataFrame elements cannot be max/min float/integer. Otherwise SDC and Pandas results are different.
518+ Different size of `self` and `other` can produce result different from the result of Pandas
519+ due to different float rounding in Python and SDC.
520+ """ ,
521+ 'extra_params' :
522+ """
523+ other: :obj:`Series` or :obj:`DataFrame`
524+ Other Series or DataFrame.
525+ pairwise: :obj:`bool`
526+ Calculate pairwise combinations of columns within a DataFrame.
527+ ddof: :obj:`int`
528+ Delta Degrees of Freedom.
529+ """
530+ })
531+
460532sdc_pandas_dataframe_rolling_kurt .__doc__ = sdc_pandas_dataframe_rolling_docstring_tmpl .format (** {
461533 'method_name' : 'kurt' ,
462534 'example_caption' : 'Calculate unbiased rolling kurtosis.' ,
0 commit comments