You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2020/09/14 08:38:25 UTC

[GitHub] [spark] HyukjinKwon commented on a change in pull request #29699: [SPARK-32835][PYTHON] Add withField method to the pyspark Column class

HyukjinKwon commented on a change in pull request #29699:
URL: https://github.com/apache/spark/pull/29699#discussion_r487744634



##########
File path: python/pyspark/sql/column.py
##########
@@ -329,6 +329,31 @@ def getField(self, name):
                 DeprecationWarning)
         return self[name]
 
+    @since(3.1)
+    def withField(self, fieldName, col):
+        """
+        An expression that adds/replaces field in StructType by name.
+
+        >>> from pyspark.sql import Row, functions
+        >>> df = spark.createDataFrame([Row(a=Row(b=1, c=2))])
+        >>> df.withColumn('a', df['a'].withField('b', functions.lit(3))).select('a.b').show()
+        +---+
+        |  b|
+        +---+
+        |  3|
+        +---+
+        >>> df.withColumn('a', df['a'].withField('d', functions.lit(4))).select('a.d').show()
+        +---+
+        |  d|
+        +---+
+        |  4|
+        +---+
+        """
+        if not isinstance(col, Column):
+            raise TypeError("col should be a Column")
+

Review comment:
       Can you check if `fieldName` is a string as well?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org