Extract date from a string column containing timestamp in Pyspark

后端 未结 1 960
长发绾君心
长发绾君心 2021-02-10 10:15

I have a dataframe which has a date in the following format:

+----------------------+
|date                  |
+----------------------+
|May 6, 2016 5:59:34 AM|
         


        
1条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-10 10:56

    There are some errors with your pattern. Here's a suggestion:

    from_pattern = 'MMM d, yyyy h:mm:ss aa'
    to_pattern = 'yyyy-MM-dd'
    df.withColumn('part_date', from_unixtime(unix_timestamp(df['date'], from_pattern), to_pattern)).show()
    
    +----------------------+----------+
    |date                  |part_date |
    +----------------------+----------+
    |May 6, 2016 5:59:34 AM|2016-05-06|
    +----------------------+----------+
    

    0 讨论(0)
提交回复
热议问题