pyspark.sql.functions.date_sub#

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

Returns the date that is days days before start. If days is a negative value then these amount of days will be added to start.

Added in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters:
startColumn or column name

date column to work on. A column that evaluates to a date.

daysColumn or column name or int

how many days before the given date to calculate. Accepts negative value as well to calculate forward in time. A column that evaluates to an integer, short, or byte.

Returns:
Column

a date before/after given number of days. Returns a column that evaluates to a date.

Examples

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