在pyspark中,有没有一种方法可以将timestamp数据类型的dataframe列转换为格式为'YYYY-MM-DD'的字符串?

最佳答案

如果您有一列的schema

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

然后,您可以使用from_unixtime函数将时间戳转换为bigInt之后,使用unix_timestamp函数将时间戳转换为字符串
from pyspark.sql import functions as f
df.withColumn("date", f.from_unixtime(f.unix_timestamp(df.date), "yyyy-MM-dd"))

你应该有
root
 |-- date: string (nullable = true)

关于apache-spark - pyspark将dataframe列从时间戳转换为 "YYYY-MM-DD"格式的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48910511/

10-12 07:39
查看更多