You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@spark.apache.org by GitBox <gi...@apache.org> on 2022/04/04 00:48:05 UTC

[GitHub] [spark] zhengruifeng commented on a diff in pull request #36048: [SPARK-38774][PYTHON] Implement Series.autocorr

zhengruifeng commented on code in PR #36048:
URL: https://github.com/apache/spark/pull/36048#discussion_r841305123


##########
python/pyspark/pandas/series.py:
##########
@@ -2937,6 +2937,73 @@ 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:
+        """
+        Compute the lag-N autocorrelation.
+
+        This method computes the Pearson correlation between
+        the Series and its shifted self.
+
+        Parameters
+        ----------
+        periods : int, default 1
+            Number of lags to apply before performing autocorrelation.
+
+        Returns
+        -------
+        float
+            The Pearson correlation between self and self.shift(lag).
+
+        See Also
+        --------
+        Series.corr : Compute the correlation between two Series.
+        Series.shift : Shift index by desired number of periods.
+        DataFrame.corr : Compute pairwise correlation of columns.
+
+        Notes
+        -----
+        If the Pearson correlation is not well defined return 'NaN'.
+
+        Examples
+        --------
+        >>> s = ps.Series([.2, .0, .6, .2, np.nan, .5, .6])
+        >>> s.autocorr()  # doctest: +ELLIPSIS
+        -0.141219...
+        >>> s.autocorr(0)  # doctest: +ELLIPSIS
+        1.0...
+        >>> s.autocorr(2)  # doctest: +ELLIPSIS
+        0.970725...
+        >>> s.autocorr(-3)  # doctest: +ELLIPSIS
+        0.277350...
+        >>> s.autocorr(5)  # doctest: +ELLIPSIS
+        -1.000000...
+        >>> s.autocorr(6)  # doctest: +ELLIPSIS
+        nan
+
+        If the Pearson correlation is not well defined, then 'NaN' is returned.
+
+        >>> s = ps.Series([1, 0, 0, 0])
+        >>> s.autocorr()
+        nan
+        """
+        # 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__)
+
+        scol = self.spark.column.alias("__tmp_col__")
+        if periods == 0:
+            lag_col = scol.alias("__tmp_lag_col__")
+        else:
+            window = Window.orderBy(NATURAL_ORDER_COLUMN_NAME)
+            lag_col = F.lag(scol, periods).over(window).alias("__tmp_lag_col__")
+
+        return (
+            self._internal.spark_frame.select([scol, lag_col])
+            .dropna("any")
+            .corr("__tmp_col__", "__tmp_lag_col__")

Review Comment:
   good point, will update soon



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe e-mail: dev-unsubscribe@spark.apache.org