0

I'm trying to convert a string type column to date type column using udf functions as given below

Example input column value: JAN 2018

Expected output value: 01-01-2018

here is my code

from datetime import datetime

from pyspark.sql.types import DateType

squared_udf = udf(lambda z: datetime.strptime(z,'%b %Y').strftime('%Y-%m-%d'), DateType())

df = df.select('TIME PERIOD', squared_udf('TIME PERIOD'))

Output of my code:

DataFrame[TIME PERIOD: string, (TIME PERIOD): date]

But I'm expecting spark dataframe updated with TIME PERIOD column

Please suggest on the same.

Safvan CK
  • 1,140
  • 9
  • 18
Pooja
  • 11
  • 4
  • use to_date() and date_format() functions: `df.selectExpr('date_format(to_date("JAN 2018", "MMM yyyy"),"MM-dd-yyyy")')` – jxc Sep 17 '19 at 11:05
  • Possible duplicate of [Convert pyspark string to date format](https://stackoverflow.com/questions/38080748/convert-pyspark-string-to-date-format) – pault Sep 17 '19 at 14:25

0 Answers0