Skip to content

[WIP][SPARK-52394][PS] Fix autocorr divide-by-zero error under ANSI mode #51192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xinrong-meng
Copy link
Member

@xinrong-meng xinrong-meng commented Jun 16, 2025

What changes were proposed in this pull request?

Fix autocorr divide-by-zero error under ANSI mode

Why are the changes needed?

Ensure pandas on Spark works well with ANSI mode on.
Part of https://issues.apache.org/jira/browse/SPARK-52169.

Does this PR introduce any user-facing change?

How was this patch tested?

>>> import pandas as pd
>>> import numpy as np
>>> 
>>> ps.set_option("compute.fail_on_ansi_mode", False)
>>> ps.set_option("compute.ansi_mode_support", True)
>>> 
>>> s = ps.Series([.2, .0, .6, .2, np.nan, .5, .6])
>>> s.autocorr()
-0.14231876063832774
>>> s.autocorr(0)
1.0
>>> s.autocorr(2)
0.09234860641727351
>>> s.autocorr(-3)
0.1701242227446561
>>> s.autocorr(5)
-0.14085904245475267
>>> s.autocorr(6)
nan
>>> quit()

Was this patch authored or co-authored using generative AI tooling?

else:
lag_scol = F.lag(scol, lag).over(Window.orderBy(NATURAL_ORDER_COLUMN_NAME))
lag_col_name = verify_temp_column_name(sdf, "__autocorr_lag_tmp_col__")
corr = (
sdf.withColumn(lag_col_name, lag_scol)
.select(F.corr(scol, F.col(lag_col_name)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does corr affected by ansi?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants