Skip to content

Commit 605ddbb

Browse files
Davies Liurxin
authored andcommitted
[SPARK-8038] [SQL] [PYSPARK] fix Column.when() and otherwise()
Thanks ogirardot, closes apache#6580 cc rxin JoshRosen Author: Davies Liu <[email protected]> Closes apache#6590 from davies/when and squashes the following commits: c0f2069 [Davies Liu] fix Column.when() and otherwise()
1 parent 686a45f commit 605ddbb

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

python/pyspark/sql/column.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ def between(self, lowerBound, upperBound):
315315
"""
316316
A boolean expression that is evaluated to true if the value of this
317317
expression is between the given columns.
318+
319+
>>> df.select(df.name, df.age.between(2, 4)).show()
320+
+-----+--------------------------+
321+
| name|((age >= 2) && (age <= 4))|
322+
+-----+--------------------------+
323+
|Alice| true|
324+
| Bob| false|
325+
+-----+--------------------------+
318326
"""
319327
return (self >= lowerBound) & (self <= upperBound)
320328

@@ -328,12 +336,20 @@ def when(self, condition, value):
328336
329337
:param condition: a boolean :class:`Column` expression.
330338
:param value: a literal value, or a :class:`Column` expression.
339+
340+
>>> from pyspark.sql import functions as F
341+
>>> df.select(df.name, F.when(df.age > 4, 1).when(df.age < 3, -1).otherwise(0)).show()
342+
+-----+--------------------------------------------------------+
343+
| name|CASE WHEN (age > 4) THEN 1 WHEN (age < 3) THEN -1 ELSE 0|
344+
+-----+--------------------------------------------------------+
345+
|Alice| -1|
346+
| Bob| 1|
347+
+-----+--------------------------------------------------------+
331348
"""
332-
sc = SparkContext._active_spark_context
333349
if not isinstance(condition, Column):
334350
raise TypeError("condition should be a Column")
335351
v = value._jc if isinstance(value, Column) else value
336-
jc = sc._jvm.functions.when(condition._jc, v)
352+
jc = self._jc.when(condition._jc, v)
337353
return Column(jc)
338354

339355
@since(1.4)
@@ -345,9 +361,18 @@ def otherwise(self, value):
345361
See :func:`pyspark.sql.functions.when` for example usage.
346362
347363
:param value: a literal value, or a :class:`Column` expression.
364+
365+
>>> from pyspark.sql import functions as F
366+
>>> df.select(df.name, F.when(df.age > 3, 1).otherwise(0)).show()
367+
+-----+---------------------------------+
368+
| name|CASE WHEN (age > 3) THEN 1 ELSE 0|
369+
+-----+---------------------------------+
370+
|Alice| 0|
371+
| Bob| 1|
372+
+-----+---------------------------------+
348373
"""
349374
v = value._jc if isinstance(value, Column) else value
350-
jc = self._jc.otherwise(value)
375+
jc = self._jc.otherwise(v)
351376
return Column(jc)
352377

353378
@since(1.4)

0 commit comments

Comments
 (0)