Converting yyyymmdd to MM-dd-yyyy format in pyspark

前端 未结 2 1666
情深已故
情深已故 2021-01-15 04:49

I have a large data frame df containing a column for date in the format yyyymmdd, how can I convert it into MM-dd-yyyy in pySpark.

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 05:07

    This is also working:

    from datetime import datetime
    from pyspark.sql.functions import col,udf,unix_timestamp
    from pyspark.sql.types import DateType
    
    
    func =  udf(lambda x: datetime.strptime(str(x), '%m%d%y'), DateType())
    
    df2 = df.withColumn('date', func(col('InvcDate')))
    

提交回复
热议问题