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/04/15 20:25:58 UTC
[GitHub] [spark] xinrong-databricks commented on a diff in pull request #36205: [SPARK-38907][PYTHON] Implement DataFrame.corrwith
xinrong-databricks commented on code in PR #36205:
URL: https://github.com/apache/spark/pull/36205#discussion_r851500660
##########
python/pyspark/pandas/frame.py:
##########
@@ -1310,6 +1310,138 @@ def corr(self, method: str = "pearson") -> "DataFrame":
"""
return cast(DataFrame, ps.from_pandas(corr(self, method)))
+ def corrwith(
+ self, other: DataFrameOrSeries, drop: bool = False, method: str = "pearson"
+ ) -> "Series":
+ """
+ Compute pairwise correlation.
+
+ Pairwise correlation is computed between rows or columns of
+ DataFrame with rows or columns of Series or DataFrame. DataFrames
+ are first aligned along both axes before computing the
+ correlations.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ other : DataFrame, Series
+ Object with which to compute correlations.
+
+ drop : bool, default False
+ Drop missing indices from result.
+
+ method : str, default 'pearson'
+ Method of correlation, one of:
+
+ * pearson : standard correlation coefficient
+
+ Returns
+ -------
+ Series
+ Pairwise correlations.
+
+ See Also
+ --------
+ DataFrame.corr : Compute pairwise correlation of columns.
+
+ Examples
+ --------
+ >>> df1 = ps.DataFrame({"A":[1, 5, 7, 8],
+ ... "X":[5, 8, 4, 3],
+ ... "C":[10, 4, 9, 3]})
+ >>> df1.corrwith(df1[["X", "C"]])
+ X 1.0
+ C 1.0
+ A NaN
+ dtype: float64
+
+ >>> df2 = ps.DataFrame({"A":[5, 3, 6, 4],
+ ... "B":[11, 2, 4, 3],
+ ... "C":[4, 3, 8, 5]})
+
+ >>> with ps.option_context("compute.ops_on_diff_frames", True):
+ ... df1.corrwith(df2)
+ A -0.041703
+ C 0.395437
+ X NaN
+ B NaN
+ dtype: float64
+
+ >>> with ps.option_context("compute.ops_on_diff_frames", True):
+ ... df2.corrwith(df1.X)
+ A -0.597614
+ B -0.151186
+ C -0.642857
+ dtype: float64
+ """
+ from pyspark.pandas.series import Series
+
+ if (method is not None) and (method not in ["pearson"]):
+ raise NotImplementedError("corrwith currently works only for method='pearson'")
+ if not isinstance(other, (DataFrame, Series)):
+ raise TypeError("unsupported type: {}".format(type(other).__name__))
+
+ right_is_series = isinstance(other, Series)
+
+ if same_anchor(self, other):
+ combined = self
+ this = self
+ that = other
+ else:
+ combined = combine_frames(self, other, how="inner")
+ this = combined["this"]
+ that = combined["that"]
+
+ this_numeric_column_labels: List[Label] = []
+ for column_label in this._internal.column_labels:
+ if isinstance(this._internal.spark_type_for(column_label), (NumericType, BooleanType)):
+ this_numeric_column_labels.append(column_label)
+
+ that_numeric_column_labels: List[Label] = []
+ for column_label in that._internal.column_labels:
+ if isinstance(that._internal.spark_type_for(column_label), (NumericType, BooleanType)):
+ that_numeric_column_labels.append(column_label)
+
+ intersect_numeric_column_labels: List[Label] = []
+ diff_numeric_column_labels: List[Label] = []
+ if right_is_series:
+ intersect_numeric_column_labels = this_numeric_column_labels
+ corr_scols = []
Review Comment:
nit: Initializing `corr_scols` above the if-else may be clearer. The current code also looks fine.
--
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