You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "itholic (via GitHub)" <gi...@apache.org> on 2023/09/04 09:06:58 UTC

[GitHub] [spark] itholic commented on a diff in pull request #42798: [SPARK-43295][PS] Support string type columns for `DataFrameGroupBy.sum`

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


##########
python/pyspark/pandas/groupby.py:
##########
@@ -3534,7 +3534,12 @@ def _reduce_for_stat_function(
             for label in psdf._internal.column_labels:
                 psser = psdf._psser_for(label)
                 input_scol = psser._dtype_op.nan_to_null(psser).spark.column
-                output_scol = sfun(input_scol)
+                if sfun.__name__ == "sum" and isinstance(
+                    psdf._internal.spark_type_for(label), StringType
+                ):
+                    output_scol = F.concat_ws("", F.collect_list(input_scol))

Review Comment:
   We should use combination of `concat_ws` and `collect_list` instead of `sum` to match the behavior with Pandas for string summation as below:
   
   ```python
   >>> import pyspark.sql.functions as sf
   >>> sdf.show()
   +---+
   |  A|
   +---+
   |  a|
   |  b|
   |  c|
   +---+
   
   # Using `sum` over string type column returns `NULL` which is not matched with pandas.
   >>> sdf.select(sf.sum(sdf.A)).show()
   +------+
   |sum(A)|
   +------+
   |  NULL|
   +------+
   
   # Using combination of `concat_ws` and `collect_list` to match the pandas behavior
   >>> sdf.select(sf.concat_ws("", sf.collect_list(sdf.A))).show()
   +----------------------------+
   |concat_ws(, collect_list(A))|
   +----------------------------+
   |                         abc|
   +----------------------------+
   ```



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