spark-test commented on code in PR #60686:
URL: https://github.com/apache/spark/pull/60686#discussion_r4624208759


##########
python/pyspark/pandas/groupby.py:
##########
@@ -411,6 +411,8 @@ def _spark_groupby(
                         )
                     )
 
+                elif aggfunc == "size":

Review Comment:
   **Non-blocking (P2):** This mapping only reaches callers of 
`GroupBy._spark_groupby`. `DataFrame.pivot_table` builds its own aggregate 
expressions by applying the aggfunc name to the values column, for both the 
string and the dict form (python/pyspark/pandas/frame.py:7087-7114), so by the 
code `pivot_table(index=['A'], columns='C', values='D', aggfunc='size')` still 
produces Spark's `size(D)`. That function accepts only arrays and maps, and 
pivot_table admits only numeric values, so every such call fails analysis. 
pandas returns each cell's row count for that call, nulls included, which is 
what the named groupby form computes after this change.
   
   Mapping 'size' to `count(1)` where pivot_table builds its expressions would 
close the gap. For a string 'size' over a list of values, pandas returns one 
block of counts without a column level per values column, so that case also 
needs its own layout.
   
   **Verification:**
   - **Behavior:** Compare pivot_table with pandas for aggfunc='size' on 
numeric data that has a null value and an absent index and column combination: 
once with one values column, once with a list of two values columns, and once 
with a dict that maps one values column to 'size' and another to 'sum'. Check 
the values, column labels, and dtypes.
   - **Behavior:** Without an index, where pandas raises for 'size', compare 
pivot_table(aggfunc='size') on a values column with no null against pandas' 
pivot_table(aggfunc='count') for the same call, and, on a values column that 
holds a null, check that the 'size' counts include that row.
   - **Regression:** On a values column that holds a null, check that 
aggfunc='size' and aggfunc='count' give different counts, so that the 
comparison fails if 'size' were lowered to a null-skipping count or left as 
Spark's collection size function.
   - **Compatibility:** Run the same pandas comparisons through a Spark Connect 
session, which receives the same aggregate expression, and confirm they give 
the same results there.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to