Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions python/pyspark/pandas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3396,11 +3396,14 @@ def autocorr(self, lag: int = 1) -> float:
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?

Copy link
Member Author

@xinrong-meng xinrong-meng Aug 4, 2025

Choose a reason for hiding this comment

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

The example below shows how F.corr(col1, col2) fails with a DIVIDE_BY_ZERO error when ANSI on

>>> df = spark.createDataFrame(
...     [(1, None), (0, 1), (0, 0), (0, 0)],
...     ["val", "lag"]
... )
>>> df.show()
+---+----+
|val| lag|
+---+----+
|  1|NULL|
|  0|   1|
|  0|   0|
|  0|   0|
+---+----+
>>> df.select(F.corr("val", "lag")).show()
...
org.apache.spark.SparkArithmeticException: [DIVIDE_BY_ZERO] Division by zero. Use `try_divide` to tolerate divisor being 0 and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error. SQLSTATE: 22012
== DataFrame ==
"corr" was called from
...

>>> spark.conf.set("spark.sql.ansi.enabled", False)
>>> df.select(F.corr("val", "lag")).show()
+--------------+
|corr(val, lag)|
+--------------+
|          NULL|
+--------------+

.head()[0]
)

sdf_lag = sdf.withColumn(lag_col_name, lag_scol)
if is_ansi_mode_enabled(sdf.sparkSession):
cov_value = sdf_lag.select(F.covar_samp(scol, F.col(lag_col_name))).first()[0]
Copy link
Member

Choose a reason for hiding this comment

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

nit: Why is this using first() instead of head()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Used head for consistency.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure the relationship between these functions and corr. Could you add a comment to explain why it works?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, added comments

Copy link
Member

Choose a reason for hiding this comment

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

if cov_value is None or cov_value == 0.0:
return np.nan
corr = sdf_lag.select(F.corr(scol, F.col(lag_col_name))).head()[0]

return np.nan if corr is None else corr

def corr(
Expand Down
3 changes: 3 additions & 0 deletions python/pyspark/pandas/tests/series/test_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ def test_autocorr(self):
with self.assertRaisesRegex(TypeError, r"lag should be an int; however, got"):
psser.autocorr(1.0)

psser = ps.Series([1, 0, 0, 0])
self.assertTrue(bool(np.isnan(psser.autocorr())))

def _test_autocorr(self, pdf):
psdf = ps.from_pandas(pdf)
for lag in range(-10, 10):
Expand Down