Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions python/pyspark/pandas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ def add_suffix(self, suffix: str) -> "Series":
DataFrame(internal.with_new_sdf(sdf, index_fields=([None] * internal.index_level)))
)

def autocorr(self, periods: int = 1) -> float:
def autocorr(self, lag: int = 1) -> float:
"""
Compute the lag-N autocorrelation.

Expand All @@ -3270,7 +3270,7 @@ def autocorr(self, periods: int = 1) -> float:

Parameters
----------
periods : int, default 1
lag : int, default 1
Number of lags to apply before performing autocorrelation.

Returns
Expand Down Expand Up @@ -3312,15 +3312,15 @@ def autocorr(self, periods: int = 1) -> float:
"""
# This implementation is suboptimal because it moves all data to a single partition,
# global sort should be used instead of window, but it should be a start
if not isinstance(periods, int):
raise TypeError("periods should be an int; however, got [%s]" % type(periods).__name__)
if not isinstance(lag, int):
raise TypeError("lag should be an int; however, got [%s]" % type(lag).__name__)

sdf = self._internal.spark_frame
scol = self.spark.column
if periods == 0:
if lag == 0:
corr = sdf.select(F.corr(scol, scol)).head()[0]
else:
lag_scol = F.lag(scol, periods).over(Window.orderBy(NATURAL_ORDER_COLUMN_NAME))
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)
Expand Down
2 changes: 1 addition & 1 deletion python/pyspark/pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3240,7 +3240,7 @@ def test_autocorr(self):
self._test_autocorr(pdf)

psser = ps.from_pandas(pdf["s1"])
with self.assertRaisesRegex(TypeError, r"periods should be an int; however, got"):
with self.assertRaisesRegex(TypeError, r"lag should be an int; however, got"):
psser.autocorr(1.0)

def _test_autocorr(self, pdf):
Expand Down