pyspark convert dataframe column from timestamp to string of “YYYY-MM-DD” format

匆匆过客 提交于 2020-02-27 05:59:19

问题


In pyspark is there a way to convert a dataframe column of timestamp datatype to a string of format 'YYYY-MM-DD' format?


回答1:


If you have a column with schema as

root
 |-- date: timestamp (nullable = true)

Then you can use from_unixtime function to convert the timestamp to string after converting the timestamp to bigInt using unix_timestamp function as

from pyspark.sql import functions as f
df.withColumn("date", f.from_unixtime(f.unix_timestamp(df.date), "yyyy-MM-dd"))

and you should have

root
 |-- date: string (nullable = true)



回答2:


You can use date_format function as below

from pyspark.sql.functions import date_format

df.withColumn("dateColumn",  date_format(col("vacationdate"), "yyyy-MM-dd"))

Hope this helps!




回答3:


from pyspark.sql.functions  import date_format

df.withColumn("DateOnly", date_format('DateTime', "yyyy-MM-dd")).show()


来源:https://stackoverflow.com/questions/48910511/pyspark-convert-dataframe-column-from-timestamp-to-string-of-yyyy-mm-dd-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!