小编len*_*ehm的帖子

在 PySpark 中将 Unix 时间戳转换为毫秒时间戳

我的数据帧时间戳中有一列中有一个列,其中包含 UNIX 13 位时间戳,如下所示:

| 时间戳|
| ------------- |
| 1584528257638 |
| 1586618807677 |
| 1585923477767 |
| 1583314882085 |

使用 pandas 可以很容易地将其转换为:

ms = pd.to_datetime(df[column], unit='ms')
df[column] = ms
Run Code Online (Sandbox Code Playgroud)

然而,在 pySpark 中,这并不那么容易,我发现了其他一些内容,例如这篇文章试图实现这个目标。最后毫秒的串联对我不起作用,它总是导致第二个时间戳 (HH:mm:ss) 而不是 HH:mm:ss.SSS。

到目前为止我尝试过的是:

df = df.withColumn("unix_timestamp", F.unix_timestamp(df.timestamp,'yyyy-MM-dd HH:mm:ss.SSS z') + F.substring(df.timestamp, -3,3).cast('float')/1000)

df = df.withColumn("ms_Timestamp", F.to_timestamp(df["unix_timestamp"]))
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有将其转换为毫秒时间戳,我不知道还能做什么。

我将不胜感激最终获得毫秒时间戳的任何帮助。

祝一切顺利,并提前致谢。

python timestamp unix-timestamp dataframe pyspark

5
推荐指数
1
解决办法
3254
查看次数

标签 统计

dataframe ×1

pyspark ×1

python ×1

timestamp ×1

unix-timestamp ×1