ben890
ben890

Reputation: 1133

PySpark Add Months to Date Field Based on vlaue of column

I have a dataframe with a date column and an integer column and I'd like to add months based on the integer column to the date column. I tried the following, but I'm getting an error:

from pyspark.sql import functions as f
withColumn('future', f.add_months('cohort', col('period')))

Where 'cohort' is my date column and period is an integer. I'm getting the following error:

TypeError: Column is not iterable

Upvotes: 1

Views: 1510

Answers (1)

blackbishop
blackbishop

Reputation: 32720

Use expr to pass a column as second parameter for add_months function:

df.withColumn('future', F.expr("add_months(cohort, period)"))

Upvotes: 2

Related Questions