I have a date pyspark dataframe with a string column in the format of MM-dd-yyyy
and I am attempting to convert this into a date column.
I tried:
df.select(to_date(df.STRING_COLUMN).alias('new_date')).show()
and I get a string of nulls. Can anyone help?
It is possible (preferrable?) to do this without a udf:
Update (1/10/2018):
For Spark 2.2+ the best way to do this is probably using the
to_date
orto_timestamp
functions, which both support theformat
argument. From the docs:The strptime() approach does not work for me. I get another cleaner solution, using cast:
Here is the output:
Try this: