【问题标题】:Convert column of timestamp integer to Datetime that stop after the hour in a spark Dataframe将时间戳整数列转换为在火花数据帧中在一小时后停止的日期时间
【发布时间】:2021-07-19 12:07:11
【问题描述】:

我有一个类似的 spark 数据框

我将时间戳记为整数,我想创建另一个包含整个日期 + 小时的列 我不明白为什么这段代码不起作用:

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

【问题讨论】:

    标签: python datetime pyspark timestamp


    【解决方案1】:

    您可以将 Date 列转换为所需格式的时间戳。

    from pyspark.sql.functions import to_timestamp   
    
    df = df.withColumn("new_date", to_timestamp("Date", "yyyyMMdd"))
    

    注意*您也询问了小时,但我没有看到您的日期列中的小时信息。 如果您刚刚将其添加到格式中 ,例如:

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

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 2021-12-19
      • 2021-07-02
      • 2017-08-11
      • 2023-02-07
      • 2014-09-04
      • 2021-09-07
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多