pyspark.sql.functions.date_sub

pyspark.sql.functions.date_sub(start: ColumnOrName, days: Union[ColumnOrName, int]) → pyspark.sql.column.Column[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.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
startColumn or str

date column to work on.

daysColumn or str or int

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

Returns
Column

a date before/after given number of days.

Examples

>>> df = spark.createDataFrame([('2015-04-08', 2,)], ['dt', 'sub'])
>>> df.select(date_sub(df.dt, 1).alias('prev_date')).collect()
[Row(prev_date=datetime.date(2015, 4, 7))]
>>> df.select(date_sub(df.dt, df.sub.cast('integer')).alias('prev_date')).collect()
[Row(prev_date=datetime.date(2015, 4, 6))]
>>> df.select(date_sub('dt', -1).alias('next_date')).collect()
[Row(next_date=datetime.date(2015, 4, 9))]