【问题标题】:How to filter Timestamp in Hours instead of Seconds?如何以小时而不是秒为单位过滤时间戳?
【发布时间】:2019-11-18 15:34:30
【问题描述】:

timestampbeforetimestamp after @pissall's code我有一个频率为 0.5Hz 的时间戳列,这会产生数百万行。我愿意通过每小时设置一个时间戳来减少这个数据大小。即某一天的 24 次观察。 我已经通过按年、月和日过滤数据来减少数据大小。但由于它仍然很大,我现在想将其减少到每小时一次。

我正在研究 Databricks 并使用 PySpark。

我使用以下命令将我的数据大小从几年减少到一天。

df = df.filter(df.Timestamp.between('2019-09-03 00:00:00','2019-09-04 00:00:00'))

感谢您的帮助。 谢谢

Java.util.gregori...

【问题讨论】:

  • 您要汇总到小时数吗?
  • @pissall 是的,我想将数据汇总到小时。
  • 聚合是什么?总和/平均/等?
  • @pissall 并不是真正的 sum/avg 类型的聚合。相反,我想获取(聚合)每小时而不是每秒发生的值。例如:Time Stamp Latitude Longitude2019-09-03 00:00:00 132323 -3,545452019-09-03 00:00:01 xxxx yyyy2019-09-03 00:00:02 aaa aaa 而不是我想在2019-09-03 00:00:00 2019-09-03 01:00:00 2019-09-03 02:00:00 有值

标签: pyspark timestamp sampling azure-databricks


【解决方案1】:

您可以使用 UDF replace minutesseconds 部分 datetime。可能不是最好的解决方案,但你可以这样做:

import pyspark.sql.functions as F
from pyspark.sql.types import TimestampType

date_replace_udf = F.udf(lambda date: date.replace(minute=0, second=0, microsecond=0),TimestampType())

df = df.withColumn("Timestamp", date_replace_udf(F.col("Timestamp")))

另一个参考:How to truncate the time on a DateTime object in Python?

【讨论】:

  • 感谢您的快速解决方案,但它仍然返回时间戳每秒而不是每小时更改的数据帧。
  • 你能显示输出吗?请将其添加到您的问题中。
  • @LavMehta 您需要检查fixed_date 列。我正在修改答案,请重试
  • 感谢您的更正,但现在我在时间戳列下得到“java.util.Gregori ....”
  • 它没有抛出错误,而是我只能在列中看到的内容。请检查我在问题中添加的图像。
猜你喜欢
  • 2016-12-15
  • 2015-06-17
  • 2012-12-25
  • 2023-03-11
  • 2011-01-20
  • 2014-07-18
  • 2011-08-02
  • 2012-01-12
  • 1970-01-01
相关资源
最近更新 更多