简体   繁体   English

pyspark 将 dataframe 列从时间戳转换为“YYYY-MM-DD”格式的字符串

[英]pyspark convert dataframe column from timestamp to string of "YYYY-MM-DD" format

In pyspark is there a way to convert a dataframe column of timestamp datatype to a string of format 'YYYY-MM-DD' format?在 pyspark 中,有没有办法将时间戳数据类型的 dataframe 列转换为格式为“YYYY-MM-DD”格式的字符串?

You can use date_format function as below 您可以使用date_format函数,如下所示

from pyspark.sql.functions import date_format

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

Hope this helps! 希望这可以帮助!

If you have a column with schema as 如果您有一个包含schema的列

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_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"))

and you should have 你应该有

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

one other option to try out will be尝试的另一种选择是

from pyspark.sql import functions as F从 pyspark.sql 导入函数作为 F

df = df.withColumn('new_time_column', F.to_timestamp(df['Time_column'], 'yyyy-MM-dd')) df = df.withColumn('new_time_column', F.to_timestamp(df['Time_column'], 'yyyy-MM-dd'))

from pyspark.sql.functions  import date_format

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pyspark sql 将日期格式从 mm/dd/yy hh:mm 或 yyyy-mm-dd hh:mm:ss 转换为 yyyy-mm-dd hh:mm 格式 - pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format Spark SQL:如何将“yyyy-MM-dd HH:mm:ss.SSSSSSSSS”格式的时间字符串列转换为保留纳秒的时间戳? - Spark SQL: How to convert time string column in “yyyy-MM-dd HH:mm:ss.SSSSSSSSS” format to timestamp preserving nanoseconds? Pyspark-将mmddyy转换为YYYY-MM-DD - Pyspark - Convert mmddyy to YYYY-MM-DD 如何将 Pyspark 行 datetime.datetime dataframe 转换为列名 DateType 与 DD-MM-YYYY 中的时间戳 - How to convert Pyspark Row datetime.datetime dataframe to column name DateType with timestamp in DD-MM-YYYY 如何在 pyspark 数据框中以“DD/MM/YYYY”格式转换日期? - How to convert dates in "DD/MM/YYYY" format in a pyspark dataframe? Pyspark 中从字符串到日期时间 (yyyy-mm-dd hh:mm:ss) 的转换 - Conversion from String to datetime (yyyy-mm-dd hh:mm:ss) in Pyspark pyspark:如何以“yyyy-MM-dd HH”格式按日期列分区 - pyspark: how to partition by date column in format 'yyyy-MM-dd HH' 格式yyyy-MM-dd HH:mm:ss从字符串到日期格式 - Format yyyy-MM-dd HH:mm:ss from String to date Format 无法在 pyspark 中将纪元时间戳转换为“dd-mm-yyyy HH:mm:ss”格式 - Unable to convert epoch timestamp into "dd-mm-yyyy HH:mm:ss" format in pyspark Spark scala 将字符串转换为时间戳(1147880044 -> “mm/dd/yyyy HH:mm:ss” 格式) - Spark scala convert string to timestamp (1147880044 -> “mm/dd/yyyy HH:mm:ss” format)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM