Skip to content

Commit 43fb6bd

Browse files
committed
Improve scala doc and pyspark test
1 parent 5ab39cc commit 43fb6bd

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

python/pyspark/sql/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,11 @@ def test_replace(self):
18511851
.replace(False, True).first())
18521852
self.assertTupleEqual(row, (True, True))
18531853

1854+
# replace with None
1855+
row = self.spark.createDataFrame(
1856+
[(u'Alice', 10, 80.0)], schema).replace((10, 80), None).first()
1857+
self.assertTupleEqual(row, (u'Alice', None, None))
1858+
18541859
# should fail if subset is not list, tuple or None
18551860
with self.assertRaises(ValueError):
18561861
self.spark.createDataFrame(

sql/core/src/main/scala/org/apache/spark/sql/DataFrameNaFunctions.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ final class DataFrameNaFunctions private[sql](df: DataFrame) {
319319

320320
/**
321321
* (Scala-specific) Replaces values matching keys in `replacement` map.
322-
* Key and value of `replacement` map must have the same type, and
323-
* can only be doubles , strings or booleans.
322+
* Key and value of `replacement` map must satisfy one of:
323+
* 1. keys are String, values are mix of String and null
324+
* 2. keys are Boolean, values are mix of Boolean and null
325+
* 3. keys are Double, values are either all Double or all null
324326
*
325327
* {{{
326328
* // Replaces all occurrences of 1.0 with 2.0 in column "height" and "weight".
@@ -342,7 +344,8 @@ final class DataFrameNaFunctions private[sql](df: DataFrame) {
342344
return df
343345
}
344346

345-
// replacementMap is either Map[String, String] or Map[Double, Double] or Map[Boolean,Boolean]
347+
// replacementMap is either Map[String, String], Map[Double, Double], Map[Boolean,Boolean]
348+
// or value being null
346349
val replacementMap: Map[_, _] = replacement.head._2 match {
347350
case null => replacement
348351
case v: String => replacement

0 commit comments

Comments
 (0)