Home > Mobile >  PySpark Add Months to Date Field Based on vlaue of column
PySpark Add Months to Date Field Based on vlaue of column

Time:02-09

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

CodePudding user response:

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

df.withColumn('future', F.expr("add_months(cohort, period)"))
  •  Tags:  
  • Related