从Pyspark中的时间戳列中提取小时。

huangapple go评论48阅读模式
英文:

extract hour from timestamp column in pyspark

问题

我正在使用以下功能从current_timestamp中提取小时:

F.hour(F.to_timestamp(F.current_timestamp(),"yyyy-MM-dd HH:mm:ss 'UTC'"))


这将返回当前的小时数为**5**

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/bLA9E.png

而我想要结果为**05**

有任何解决方法吗?
英文:

I am using below functionality to extract hour from current_timestamp

F.hour(F.to_timestamp(F.current_timestamp(),"yyyy-MM-dd HH:mm:ss 'UTC'"))

This would return me as 5 the current hour

从Pyspark中的时间戳列中提取小时。

whereas I want the result as 05

Any solution to this would be helpfull

答案1

得分: 1

你可以在pyspark中使用lpad函数,仅在该列中包含单个数字时添加0:

df.withColumn("hour", F.lpad(F.hour(F.to_timestamp(F.current_timestamp(), "yyyy-MM-dd HH:mm:ss 'UTC'")), 2, "0"))
英文:

You can use the lpad function in pyspark to add add 0 only when you have single digits in that column:

df.withColumn("hour", F.lpad(F.hour(F.to_timestamp(F.current_timestamp(), "yyyy-MM-dd HH:mm:ss 'UTC'")), 2, "0"))

huangapple
  • 本文由 发表于 2023年4月4日 14:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定