HyukjinKwon commented on code in PR #37434:
URL: https://github.com/apache/spark/pull/37434#discussion_r940055395


##########
python/pyspark/sql/functions.py:
##########
@@ -305,6 +305,40 @@ def mean(col: "ColumnOrName") -> Column:
     return _invoke_function_over_columns("mean", col)
 
 
+def median(col: "ColumnOrName") -> Column:
+    """
+    Returns the median of the values in a group.
+
+    .. versionadded:: 3.4.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or str
+        target column that the value will be returned
+
+    Returns
+    -------
+    :class:`~pyspark.sql.Column`
+        value associated with the maximum value of ord.
+
+    Examples
+    --------
+    >>> df = spark.createDataFrame([
+    ...     ("Java", 2012, 20000), ("dotNET", 2012, 5000),
+    ...     ("Java", 2012, 22000), ("dotNET", 2012, 10000),
+    ...     ("dotNET", 2013, 48000), ("Java", 2013, 30000)],
+    ...     schema=("course", "year", "earnings"))
+    >>> df.groupby("course").agg(median("earnings")).show()
+    +------+----------------+
+    |course|median(earnings)|
+    +------+----------------+
+    |  Java|         22000.0|
+    |dotNET|         10000.0|
+    +------+----------------+
+    """
+    return _invoke_function_over_columns("median", col)

Review Comment:
   I think the existing code base use percentile_approx more often:
   
   ```
   pyspark/pandas/frame.py:            # to `[Column<'percentile_approx(A, 0.2, 
10000)'>,
   pyspark/pandas/frame.py:            # Column<'percentile_approx(B, 0.2, 
10000)'>,
   pyspark/pandas/frame.py:            # Column<'percentile_approx(A, 0.5, 
10000)'>,
   pyspark/pandas/frame.py:            # Column<'percentile_approx(B, 0.5, 
10000)'>]`
   pyspark/pandas/frame.py:                    map(F.percentile_approx, 
column_names, [percentile] * column_length)
   pyspark/pandas/frame.py:                return 
F.percentile_approx(spark_column.cast(DoubleType()), qq, accuracy)
   pyspark/pandas/generic.py:                return 
F.percentile_approx(spark_column.cast(DoubleType()), 0.5, accuracy)
   pyspark/pandas/groupby.py:                            
"percentile_approx(`{0}`, array(0.25, 0.5, 0.75)) as `{1}`".format(
   pyspark/pandas/groupby.py:            return F.percentile_approx(col, 0.5, 
accuracy)
   pyspark/pandas/plot/core.py:                F.percentile_approx(
   pyspark/pandas/series.py:                    return 
F.percentile_approx(spark_column.cast(DoubleType()), q_float, accuracy)
   ```
   
   We could switch to `percentile_approx`, and add a note here. In the test, we 
can leverage tolerance or control the elements to match the results w/ pandas's 



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