python - 将时间戳舍入到最接近的 30 秒
问题描述
我在 a 中有一个列,DF
它包含timestamp
格式(yyyy-mm-dd HH:mm:ss)。我需要四舍五入timestamp
到最近的 30 秒。
old column desired column
2016-02-09 19:31:02 2016-02-09 19:31:00
2016-02-09 19:31:35 2016-02-09 19:31:30
2016-02-09 19:31:52 2016-02-09 19:32:00
2016-02-09 19:31:28 2016-02-09 19:31:30
可以在 Pyspark 中做到这一点吗?
解决方案
如果您使用的是 spark 版本 1.5+,则可以使用pyspark.sql.functions.second()
从时间戳列中获取秒数。
import pyspark.sql.functions as f
df.withColumn("second", f.second("old_timestamp")).show()
#+-------------------+------+
#| old_timestamp|second|
#+-------------------+------+
#|2016-02-09 19:31:02| 2|
#|2016-02-09 19:31:35| 35|
#|2016-02-09 19:31:52| 52|
#|2016-02-09 19:31:28| 28|
#+-------------------+------+
一旦你有了秒部分,你可以取这个数字,除以 30,四舍五入,然后乘以 30 得到“新”秒。
df.withColumn("second", f.second("old_timestamp"))\
.withColumn("new_second", f.round(f.col("second")/30)*30)\
.show()
#+-------------------+------+----------+
#| old_timestamp|second|new_second|
#+-------------------+------+----------+
#|2016-02-09 19:31:02| 2| 0.0|
#|2016-02-09 19:31:35| 35| 30.0|
#|2016-02-09 19:31:52| 52| 60.0|
#|2016-02-09 19:31:28| 28| 30.0|
#+-------------------+------+----------+
从“新”秒开始,我们可以计算以秒为单位的偏移量,当添加到原始时间戳时,将产生所需的“四舍五入”时间戳。
df.withColumn("second", f.second("old_timestamp"))\
.withColumn("new_second", f.round(f.col("second")/30)*30)\
.withColumn("add_seconds", f.col("new_second") - f.col("second"))\
.show()
#+-------------------+------+----------+-----------+
#| old_timestamp|second|new_second|add_seconds|
#+-------------------+------+----------+-----------+
#|2016-02-09 19:31:02| 2| 0.0| -2.0|
#|2016-02-09 19:31:35| 35| 30.0| -5.0|
#|2016-02-09 19:31:52| 52| 60.0| 8.0|
#|2016-02-09 19:31:28| 28| 30.0| 2.0|
#+-------------------+------+----------+-----------+
如我们所见,该列中的负数意味着必须向下舍入原始时间。正数将增加时间。
为了将此时间添加到原始时间戳,首先使用 .将其转换为 unix 时间戳pyspark.sql.functions.unix_timestamp()
。添加后,使用 . 将结果转换回时间戳pyspark.sql.functions.from_unixtime()
。
把这一切放在一起(浓缩中间步骤):
df.withColumn(
"add_seconds",
(f.round(f.second("old_timestamp")/30)*30) - f.second("old_timestamp")
)\
.withColumn(
"new_timestamp",
f.from_unixtime(f.unix_timestamp("old_timestamp") + f.col("add_seconds"))
)\
.drop("add_seconds")\
.show()
#+-------------------+-------------------+
#| old_timestamp| new_timestamp|
#+-------------------+-------------------+
#|2016-02-09 19:31:02|2016-02-09 19:31:00|
#|2016-02-09 19:31:35|2016-02-09 19:31:30|
#|2016-02-09 19:31:52|2016-02-09 19:32:00|
#|2016-02-09 19:31:28|2016-02-09 19:31:30|
#+-------------------+-------------------+
推荐阅读
- php - 正则表达式:在 wordpress 内容中拆分
- android - SoftKeyboard 隐藏 EditText 和 Pull ActionBar
- asp.net-core - ASPNET CORE - 我可以在 IApplicationModelConvention 中添加 ControllerModel 吗?
- python - Python Panda 函数汇总组的总行数
- java - 如何使用 org.glassfish.jersey.media.multipart 从 Java 将内容和文件作为多部分表单数据发送?
- python - 如何将两个变量中的数据写入文件python
- javascript - 用状态 rgb 反应设置背景颜色
- mysql - 插入语句上的多项选择 - mySQL
- android - 如何从 Firebase 动态链接中检索短 url 链接
- reactjs - 如何使用 react-native-maps 启用倾斜?