pyspark.sql.functions.try_add¶
-
pyspark.sql.functions.
try_add
(left: ColumnOrName, right: ColumnOrName) → pyspark.sql.column.Column[source]¶ Returns the sum of left`and `right and the result is null on overflow. The acceptable input types are the same with the + operator.
New in version 3.5.0.
Examples
>>> df = spark.createDataFrame([(1982, 15), (1990, 2)], ["birth", "age"]) >>> df.select(try_add(df.birth, df.age).alias('r')).collect() [Row(r=1997), Row(r=1992)]
>>> from pyspark.sql.types import StructType, StructField, IntegerType, StringType >>> schema = StructType([ ... StructField("i", IntegerType(), True), ... StructField("d", StringType(), True), ... ]) >>> df = spark.createDataFrame([(1, '2015-09-30')], schema) >>> df = df.select(df.i, to_date(df.d).alias('d')) >>> df.select(try_add(df.d, df.i).alias('r')).collect() [Row(r=datetime.date(2015, 10, 1))]
>>> df.select(try_add(df.d, make_interval(df.i)).alias('r')).collect() [Row(r=datetime.date(2016, 9, 30))]
>>> df.select( ... try_add(df.d, make_interval(lit(0), lit(0), lit(0), df.i)).alias('r') ... ).collect() [Row(r=datetime.date(2015, 10, 1))]
>>> df.select( ... try_add(make_interval(df.i), make_interval(df.i)).alias('r') ... ).show(truncate=False) +-------+ |r | +-------+ |2 years| +-------+