zhengruifeng commented on code in PR #47343: URL: https://github.com/apache/spark/pull/47343#discussion_r1683702370
########## python/pyspark/sql/functions/builtin.py: ########## @@ -3708,17 +3776,85 @@ def regr_avgy(y: "ColumnOrName", x: "ColumnOrName") -> Column: Examples -------- - >>> from pyspark.sql import functions as sf - >>> x = (sf.col("id") % 3).alias("x") - >>> y = (sf.randn(42) + x * 10).alias("y") - >>> spark.range(0, 1000, 1, 1).select(x, y).select( - ... sf.regr_avgy("y", "x"), sf.avg("y") - ... ).show() - +-----------------+-----------------+ - | regr_avgy(y, x)| avg(y)| - +-----------------+-----------------+ - |9.980732994136...|9.980732994136...| - +-----------------+-----------------+ + Example 1: All paris are non-null + + >>> import pyspark.sql.functions as sf + >>> from pyspark.sql.types import IntegerType, StructField, StructType + >>> schema = StructType([ + ... StructField('y', IntegerType(), True), + ... StructField('x', IntegerType(), True) + ... ]) + >>> df = spark.createDataFrame([(1, 2), (2, 2), (2, 3), (2, 4)], schema) + >>> df.select(sf.regr_avgy("y", "x"), sf.avg("y")).show() + +---------------+------+ + |regr_avgy(y, x)|avg(y)| + +---------------+------+ + | 1.75| 1.75| + +---------------+------+ + + Example 2: All paris's x values are null + + >>> import pyspark.sql.functions as sf + >>> from pyspark.sql.types import IntegerType, StructField, StructType + >>> schema = StructType([ + ... StructField('y', IntegerType(), True), + ... StructField('x', IntegerType(), True) + ... ]) + >>> df = spark.createDataFrame([(1, None)], schema) + >>> df.select(sf.regr_avgy("y", "x"), sf.avg("y")).show() + +---------------+------+ + |regr_avgy(y, x)|avg(y)| + +---------------+------+ + | NULL| 1.0| + +---------------+------+ + + Example 3: All paris's y values are null + + >>> import pyspark.sql.functions as sf + >>> from pyspark.sql.types import IntegerType, StructField, StructType + >>> schema = StructType([ + ... StructField('y', IntegerType(), True), + ... StructField('x', IntegerType(), True) + ... ]) + >>> df = spark.createDataFrame([(None, 1)], schema) + >>> df.select(sf.regr_avgy("y", "x"), sf.avg("y")).show() + +---------------+------+ + |regr_avgy(y, x)|avg(y)| + +---------------+------+ + | NULL| NULL| + +---------------+------+ + + Example 4: Some paris's x values are null + + >>> import pyspark.sql.functions as sf + >>> from pyspark.sql.types import IntegerType, StructField, StructType + >>> schema = StructType([ + ... StructField('y', IntegerType(), True), + ... StructField('x', IntegerType(), True) + ... ]) + >>> df = spark.createDataFrame([(1, 2), (2, None), (2, 3), (2, 4)], schema) + >>> df.select(sf.regr_avgy("y", "x"), sf.avg("y")).show() + +------------------+------+ + | regr_avgy(y, x)|avg(y)| + +------------------+------+ + |1.6666666666666667| 1.75| Review Comment: The output may vary with different envs, e.g. different CPUs -- 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-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org