This is an automated email from the ASF dual-hosted git repository. gurwls223 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push: new b0791b513da3 [SPARK-45223][PYTHON][DOCS] Refine docstring of `Column.when` b0791b513da3 is described below commit b0791b513da3f0671417b9fbcd3a0caddbb45318 Author: Hyukjin Kwon <gurwls...@apache.org> AuthorDate: Tue Nov 7 13:51:37 2023 -0800 [SPARK-45223][PYTHON][DOCS] Refine docstring of `Column.when` ### What changes were proposed in this pull request? This PR proposes to improve the docstring of `Column.when`. ### Why are the changes needed? For end users, and better usability of PySpark. ### Does this PR introduce _any_ user-facing change? Yes, it fixes the user facing documentation. ### How was this patch tested? Manually tested. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #43688 from HyukjinKwon/SPARK-45223. Authored-by: Hyukjin Kwon <gurwls...@apache.org> Signed-off-by: Hyukjin Kwon <gurwls...@apache.org> --- python/pyspark/sql/column.py | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/python/pyspark/sql/column.py b/python/pyspark/sql/column.py index 203e53474f74..9357b4842bbd 100644 --- a/python/pyspark/sql/column.py +++ b/python/pyspark/sql/column.py @@ -1388,10 +1388,12 @@ class Column: Examples -------- + Example 1: Using :func:`when` with conditions and values to create a new Column + >>> from pyspark.sql import functions as sf - >>> df = spark.createDataFrame( - ... [(2, "Alice"), (5, "Bob")], ["age", "name"]) - >>> df.select(df.name, sf.when(df.age > 4, 1).when(df.age < 3, -1).otherwise(0)).show() + >>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], ["age", "name"]) + >>> result = df.select(df.name, sf.when(df.age > 4, 1).when(df.age < 3, -1).otherwise(0)) + >>> result.show() +-----+------------------------------------------------------------+ | name|CASE WHEN (age > 4) THEN 1 WHEN (age < 3) THEN -1 ELSE 0 END| +-----+------------------------------------------------------------+ @@ -1399,6 +1401,38 @@ class Column: | Bob| 1| +-----+------------------------------------------------------------+ + Example 2: Chaining multiple :func:`when` conditions + + >>> from pyspark.sql import functions as sf + >>> df = spark.createDataFrame([(1, "Alice"), (4, "Bob"), (6, "Charlie")], ["age", "name"]) + >>> result = df.select( + ... df.name, + ... sf.when(df.age < 3, "Young").when(df.age < 5, "Middle-aged").otherwise("Old") + ... ) + >>> result.show() + +-------+---------------------------------------------------------------------------+ + | name|CASE WHEN (age < 3) THEN Young WHEN (age < 5) THEN Middle-aged ELSE Old END| + +-------+---------------------------------------------------------------------------+ + | Alice| Young| + | Bob| Middle-aged| + |Charlie| Old| + +-------+---------------------------------------------------------------------------+ + + Example 3: Using literal values as conditions + + >>> from pyspark.sql import functions as sf + >>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], ["age", "name"]) + >>> result = df.select( + ... df.name, sf.when(sf.lit(True), 1).otherwise( + ... sf.raise_error("unreachable")).alias("when")) + >>> result.show() + +-----+----+ + | name|when| + +-----+----+ + |Alice| 1| + | Bob| 1| + +-----+----+ + See Also -------- pyspark.sql.functions.when --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org