Why just the Jan works when try to convert using the code below?
df2 = spark.createDataFrame([["05-Nov-2000"], ["02-Jan-2021"]], ["date"])
df2 = df2.withColumn("date", to_date(col("date"), "D-MMM-yyyy"))
display(df2)
Result:
Date
------------
undefined
2021-01-02
Dis a day of year.The first one works because
02is in fact in January, but05is not in November.If you try:
It will work for both.
However, you need
dwhich is the day of the month. So used-MMM-yyyy.For further information please see: https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html