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/30 09:08:26 UTC

[GitHub] [spark] zhengruifeng opened a new pull request, #38060: [SPARK-40621][PS] Implement `numeric_only` and `min_count` in `GroupBy.sum`

zhengruifeng opened a new pull request, #38060:
URL: https://github.com/apache/spark/pull/38060

   ### What changes were proposed in this pull request?
   Implement `numeric_only` and `min_count` in `GroupBy.sum`
   
   
   ### Why are the changes needed?
   for API coverage
   
   
   ### Does this PR introduce _any_ user-facing change?
   new parameter
   
   ```
   In [2]: df = ps.DataFrame({"A": [1, 2, 1, 2], "B": [True, False, False, True], "C": [3, 4, 3, 4], "D": ["a", "a", "b", "a"]})
   
   In [3]: df.groupby("A").sum(numeric_only=False).sort_index()
   /Users/ruifeng.zheng/Dev/spark/python/pyspark/pandas/utils.py:975: PandasAPIOnSparkAdviceWarning: GroupBy.sum() can only support numeric and bool columns even ifnumeric_only=False, skip unsupported columns: ['D']
     warnings.warn(message, PandasAPIOnSparkAdviceWarning)
                                                                                   
      B  C
   A      
   1  1  6
   2  1  8
   
   In [4]: df._to_pandas().groupby("A").sum(numeric_only=False).sort_index()
   Out[4]: 
      B  C   D
   A          
   1  1  6  ab
   2  1  8  aa
   
   In [5]: df.groupby("D").sum(min_count=3).sort_index()
   Out[5]: 
        A    B     C
   D                
   a  5.0  2.0  11.0
   b  NaN  NaN   NaN
   
   In [6]: df._to_pandas().groupby("D").sum(min_count=3).sort_index()
   Out[6]: 
        A    B     C
   D                
   a  5.0  2.0  11.0
   b  NaN  NaN   NaN
   
   
   ```
   
   
   
   ### How was this patch tested?
   added UT


-- 
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


[GitHub] [spark] zhengruifeng commented on pull request #38060: [SPARK-40621][PS] Implement `numeric_only` and `min_count` in `GroupBy.sum`

Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #38060:
URL: https://github.com/apache/spark/pull/38060#issuecomment-1264576598

   @HyukjinKwon thank you for reviewing!


-- 
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


[GitHub] [spark] HyukjinKwon commented on pull request #38060: [SPARK-40621][PS] Implement `numeric_only` and `min_count` in `GroupBy.sum`

Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on PR #38060:
URL: https://github.com/apache/spark/pull/38060#issuecomment-1263410117

   Merged to master.


-- 
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


[GitHub] [spark] zhengruifeng commented on a diff in pull request #38060: [SPARK-40621][PS] Implement `numeric_only` and `min_count` in `GroupBy.sum`

Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on code in PR #38060:
URL: https://github.com/apache/spark/pull/38060#discussion_r984385984


##########
python/pyspark/pandas/groupby.py:
##########
@@ -806,28 +806,92 @@ def std(col: Column) -> Column:
             bool_to_numeric=True,
         )
 
-    def sum(self) -> FrameLike:
+    def sum(self, numeric_only: Optional[bool] = True, min_count: int = 0) -> FrameLike:
         """
         Compute sum of group values
 
+        .. versionadded:: 3.3.0
+
+        Parameters
+        ----------
+        numeric_only : bool, default False
+            Include only float, int, boolean columns. If None, will attempt to use
+            everything, then use only numeric data.
+            It takes no effect since only numeric columns can be support here.
+
+            .. versionadded:: 3.4.0
+        min_count: int, default 0
+            The required number of valid values to perform the operation.
+            If fewer than min_count non-NA values are present the result will be NA.
+
+            .. versionadded:: 3.4.0
+
         Examples
         --------
         >>> df = ps.DataFrame({"A": [1, 2, 1, 2], "B": [True, False, False, True],
-        ...                    "C": [3, 4, 3, 4], "D": ["a", "b", "b", "a"]})
+        ...                    "C": [3, 4, 3, 4], "D": ["a", "a", "b", "a"]})
 
-        >>> df.groupby("A").sum()
+        >>> df.groupby("A").sum().sort_index()
            B  C
         A
         1  1  6
         2  1  8
 
+        >>> df.groupby("D").sum().sort_index()
+           A  B   C
+        D
+        a  5  2  11
+        b  1  0   3
+
+        >>> df.groupby("D").sum(min_count=3).sort_index()
+             A    B     C
+        D
+        a  5.0  2.0  11.0
+        b  NaN  NaN   NaN
+
+        Notes
+        -----
+        There is a behavior difference between pandas-on-Spark and pandas:
+
+        * when there is a non-numeric aggregation column, it will be ignored
+            even if `numeric_only` is False.
+
         See Also
         --------
         pyspark.pandas.Series.groupby
         pyspark.pandas.DataFrame.groupby
         """
+        if numeric_only is not None and not isinstance(numeric_only, bool):
+            raise TypeError("numeric_only must be None or bool")
+        if not isinstance(min_count, int):
+            raise TypeError("min_count must be integer")
+
+        if numeric_only is not None and not numeric_only:
+            unsupported = [

Review Comment:
   given a non-numeric column, for example, `str` type, the final result is sensitive to the order, so not easy to implement for now.
   Right now, warn the users that such columns will be skiped:
   `PandasAPIOnSparkAdviceWarning: GroupBy.sum() can only support numeric and bool columns even ifnumeric_only=False, skip unsupported columns: ['D']`



-- 
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


[GitHub] [spark] HyukjinKwon closed pull request #38060: [SPARK-40621][PS] Implement `numeric_only` and `min_count` in `GroupBy.sum`

Posted by GitBox <gi...@apache.org>.
HyukjinKwon closed pull request #38060: [SPARK-40621][PS] Implement `numeric_only` and `min_count` in `GroupBy.sum`
URL: https://github.com/apache/spark/pull/38060


-- 
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