【问题标题】:Converting epoch time in milliseconds to time stamp to UTC error将纪元时间(以毫秒为单位)转换为时间戳到 UTC 错误
【发布时间】:2021-03-01 03:51:57
【问题描述】:

我目前正在尝试将以毫秒为单位的纪元格式存储的日期转换为时间戳。

    .withColumn(
        "date_var",
        from_utc_timestamp(
            from_unixtime("timestamp_var" / 1000, "yyyy-MM-dd HH:mm:ss"), "PST"
        )

不幸的是,这是错误的:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-df435784865f> in <module>
     32         "date_var",
     33         from_utc_timestamp(
---> 34             from_unixtime("timestamp_var" / 1000, "yyyy-MM-dd HH:mm:ss"), "PST"
     35         ),
     36     )

TypeError: unsupported operand type(s) for /: 'str' and 'int'

我不确定我的语法有什么问题,但欢迎任何帮助!

谢谢!

【问题讨论】:

    标签: python datetime unix pyspark epoch


    【解决方案1】:

    您需要选择列并将其除以 1000。Pyspark 无法将字符串除以 1000。

    import pyspark.sql.functions as F
    
    df.withColumn(
            "date_var",
            F.from_utc_timestamp(
                F.from_unixtime(F.col("timestamp_var") / 1000,
                                "yyyy-MM-dd HH:mm:ss"),
                "PST"
            )
    

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2014-03-14
      • 1970-01-01
      • 2013-08-19
      • 2015-06-17
      • 2012-03-08
      • 2013-05-14
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多