pyspark.sql.functions.date_add#

pyspark.sql.functions.date_add(start, days)[source]#

Returns the date that is days days after start. If days is a negative value then these amount of days will be deducted from start.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
startColumn or column name

date column to work on.

daysColumn or column name or int

how many days after the given date to calculate. Accepts negative value as well to calculate backwards in time.

Returns
Column

a date after/before given number of days.

Examples

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('2015-04-08', 2,)], 'struct<dt:string,a:int>')
>>> df.select('*', sf.date_add(df.dt, 1)).show()
+----------+---+---------------+
|        dt|  a|date_add(dt, 1)|
+----------+---+---------------+
|2015-04-08|  2|     2015-04-09|
+----------+---+---------------+
>>> df.select('*', sf.date_add('dt', 'a')).show()
+----------+---+---------------+
|        dt|  a|date_add(dt, a)|
+----------+---+---------------+
|2015-04-08|  2|     2015-04-10|
+----------+---+---------------+
>>> df.select('*', sf.date_add('dt', sf.lit(-1))).show()
+----------+---+----------------+
|        dt|  a|date_add(dt, -1)|
+----------+---+----------------+
|2015-04-08|  2|      2015-04-07|
+----------+---+----------------+