PineNuts0
PineNuts0

Reputation: 5234

Python PySpark: Subtract Integer Column from Date Column Error: Column Object not Callable

I am using PySpark. I have a column that is a date datatype column and another column that is an integer datatype column.

See sample below:

date         subtract
2019-01-08   7
2019-01-04   2

I want to create a new column called "new_date" that subtracts the "subtract" column value from the "date" col.

Below is my desired output:

date         subtract  new_date   
2019-01-08   7         2019-01-01
2019-01-04   2         2019-01-02

I tried the code below:

df = df.withColumn('new_date', F.date_sub(df.date, df.subtract))

Below is the error I get: TypeError: 'Column' object is not callable

Upvotes: 1

Views: 1103

Answers (1)

Neetha Mary Paul
Neetha Mary Paul

Reputation: 11

Try this:

df.withColumn("new_date", F.expr("date_sub(date, subtract)"))

Upvotes: 1

Related Questions