add_months

add_months #

pyspark.sql.functions.add_months(start, months) #

version: since 1.5.0

Returns the date that is months months after start

start: date column

months: integer

add_months

Runnable Code:

from pyspark.sql import functions as F
# Set up dataframe
data = [{"date": '2047-04-08'},
        {"date": '1999-12-31'},
        {"date": '1906-02-28'}]
df = spark.createDataFrame(data)
df = df.select(F.to_date(df.date, 'yyyy-MM-dd')
               .alias("date"))
# Use function
df = (df
     .withColumn("add_months",
                 F.add_months(F.col("date"),3))
     )
df.show()
date add_months
2047-04-08 2047-07-08
1999-12-31 2000-03-31
1906-02-28 1906-05-28

Usage:

Simple function. Adds a specified number of months to a date.



returns: Column(sc.\_jvm.functions.add_months(\_to_java_column(start), months))

PySpark manual

tags: month, date math, date addition




© 2023 PySpark Is Rad