简体   繁体   English

将时间戳整数列转换为在火花数据帧中一小时后停止的日期时间

[英]Convert column of timestamp integer to Datetime that stop after the hour in a spark Dataframe

I have a spark dataframe like that我有一个像这样的火花数据框

在此处输入图片说明

I have timestamp as integer and I want to create another column with the entire date + the hour I don't understand why this code doesn't work :我有一个整数时间戳,我想用整个日期+小时创建另一列我不明白为什么这段代码不起作用:

df = df.withColumn("Date_per_hour", df.cast('date'))

You can cast the Date column to timestamp with the required format.您可以将日期列转换为所需格式的时间戳。

from pyspark.sql.functions import to_timestamp   

df = df.withColumn("new_date", to_timestamp("Date", "yyyyMMdd"))

Note* you asked for the hour also but I don't see that you have in your Date column hour information.注意*您还询问了小时,但我没有看到您的日期列中包含小时信息。 If you have just add it to the format ,for example:如果您刚刚将其添加到格式中,例如:

df = df.withColumn("new_date", to_timestamp("Date", "yyyyMMdd HHmmss"))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM