Add months to date column in Spark dataframe

Viewed 29772

I have a scenario where I want to add months to a date column in spark DataFrame which has two columns with data type (Date, Int)

e.g.

df.show()
data_date months_to_add
2015-06-23 5
2016-07-20 7

I want to add a new column which will have a new date (After adding months to existing date) and output will look like below-

data_date month_to_add new_data_date
2015-06-23 5           2015-11-23
2016-07-20 1           2016-8-20

I have tried below piece of code, but it does not seems to be working-

df = df.withColumn("new_data_date", a
  dd_months(col("data_date"), col("months_to_add")))

it gives me error-

'Column' object is not callable

Please help me if there is any method to achieve this without using SQL query on top of dataframe.

3 Answers
Related