You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/09/21 06:29:46 UTC

[GitHub] [spark] itholic commented on a diff in pull request #37953: [SPARK-40510][PS] Implement `ddof` in `Series.cov`

itholic commented on code in PR #37953:
URL: https://github.com/apache/spark/pull/37953#discussion_r976082796


##########
python/pyspark/pandas/series.py:
##########
@@ -1002,19 +1007,23 @@ def cov(self, other: "Series", min_periods: Optional[int] = None) -> float:
         Examples
         --------
         >>> from pyspark.pandas.config import set_option, reset_option
-        >>> set_option("compute.ops_on_diff_frames", True)
         >>> s1 = ps.Series([0.90010907, 0.13484424, 0.62036035])
         >>> s2 = ps.Series([0.12528585, 0.26962463, 0.51111198])
-        >>> s1.cov(s2)
-        -0.016857626527158744
-        >>> reset_option("compute.ops_on_diff_frames")
+        >>> with ps.option_context("compute.ops_on_diff_frames", True):
+        ...     s1.cov(s2)
+        -0.016857...
+        >>> with ps.option_context("compute.ops_on_diff_frames", True):
+        ...     s1.cov(s2, ddof=2)
+        -0.033715...
         """
         if not isinstance(other, Series):
             raise TypeError("unsupported type: %s" % type(other))
         if not np.issubdtype(self.dtype, np.number):  # type: ignore[arg-type]
             raise TypeError("unsupported dtype: %s" % self.dtype)
         if not np.issubdtype(other.dtype, np.number):  # type: ignore[arg-type]
             raise TypeError("unsupported dtype: %s" % other.dtype)
+        if not isinstance(ddof, int):
+            raise TypeError("ddof must be integer")

Review Comment:
   Maybe do we need to add a negative test case?



##########
python/pyspark/pandas/series.py:
##########
@@ -993,6 +993,11 @@ def cov(self, other: "Series", min_periods: Optional[int] = None) -> float:
             Series with which to compute the covariance.
         min_periods : int, optional
             Minimum number of observations needed to have a valid result.
+        ddof : int, default 1
+            Delta degrees of freedom.  The divisor used in calculations

Review Comment:
   nit: there are two spaces between sentences.



##########
python/pyspark/pandas/series.py:
##########
@@ -1029,7 +1038,9 @@ def cov(self, other: "Series", min_periods: Optional[int] = None) -> float:
         if len(sdf.head(min_periods)) < min_periods:
             return np.nan
         else:
-            return sdf.select(F.covar_samp(*sdf.columns)).head(1)[0][0]
+            return sdf.select(SF.covar(F.col(sdf.columns[0]), F.col(sdf.columns[1]), ddof)).head(1)[
+                0
+            ][0]

Review Comment:
   Can we make it a bit more prettier?
   
   e.g.
   
   ```python
               return sdf.select(
                   SF.covar(F.col(sdf.columns[0]), F.col(sdf.columns[1]), ddof)).head(1)[0][0]
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org