File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -3396,11 +3396,14 @@ def autocorr(self, lag: int = 1) -> float:
3396
3396
else :
3397
3397
lag_scol = F .lag (scol , lag ).over (Window .orderBy (NATURAL_ORDER_COLUMN_NAME ))
3398
3398
lag_col_name = verify_temp_column_name (sdf , "__autocorr_lag_tmp_col__" )
3399
- corr = (
3400
- sdf .withColumn (lag_col_name , lag_scol )
3401
- .select (F .corr (scol , F .col (lag_col_name )))
3402
- .head ()[0 ]
3403
- )
3399
+
3400
+ sdf_lag = sdf .withColumn (lag_col_name , lag_scol )
3401
+ if is_ansi_mode_enabled (sdf .sparkSession ):
3402
+ cov_value = sdf_lag .select (F .covar_samp (scol , F .col (lag_col_name ))).first ()[0 ]
3403
+ if cov_value is None or cov_value == 0.0 :
3404
+ return np .nan
3405
+ corr = sdf_lag .select (F .corr (scol , F .col (lag_col_name ))).head ()[0 ]
3406
+
3404
3407
return np .nan if corr is None else corr
3405
3408
3406
3409
def corr (
You can’t perform that action at this time.
0 commit comments