scala - 在 Scala 中仅过滤特定格式的日期
问题描述
我有一个数据框,其中一些字段的日期格式为 D.HH:mm:ss、D.HH:mm:ss.SSSSSSS & HH:mm:ss.SSSSSSS。我只需要过滤 HH:mm:ss.SSSSSSS 类型的日期,然后将此日期转换为秒(整数)。
我在下面编写了将日期转换为秒的 scala 代码。我需要帮助来过滤特定格式的日期类型(HH:mm:ss.SSSSSSS)并跳过数据框中的其他日期格式。任何帮助,将不胜感激。
def hoursToSeconds(a: Any): Int = {
val sec = a.toString.split('.')
val fields = sec(0).split(':')
val creationSeconds = fields(0).toInt*3600 + fields(1).toInt*60 + fields(2).toInt
return creationSeconds
}
解决方案
任务可以分为两部分:
创建一些测试数据:
val df = Seq(
("one", "1.09:39:26"),
("two", "1.09:39:26.1234567"),
("three", "09:39:26.1234567")
).toDF("info", "time")
regexp和udf的定义:
val pattern = "\\A(\\d{1,2}):(\\d{2}):(\\d{2})\\.\\d{7}\\z".r
val toSeconds = udf{in: String => {
val pattern(hour, minute, second) = in
hour.toInt * 60 * 60 + minute.toInt * 60 + second.toInt
}}
实际代码:
df
.filter('time rlike pattern.regex)
.select('info, 'time, toSeconds('time).as("seconds"))
.show
印刷
+-----+----------------+-------+
| info| time|seconds|
+-----+----------------+-------+
|three|09:39:26.1234567| 34766|
+-----+----------------+-------+
如果应该保留不具有正确格式的行,可以稍微更改 udf 并且必须删除过滤器:
val pattern = "\\A(\\d{1,2}):(\\d{2}):(\\d{2})\\.\\d{7}\\z".r
val toSeconds = udf{in: String => {
in match {
case pattern(hour, minute, second)=> hour.toInt * 60 * 60 + minute.toInt * 60 + second.toInt
case _ => 0
}
}}
df
.select('info, 'time, toSeconds('time).as("seconds"))
.show
印刷
+-----+------------------+-------+
| info| time|seconds|
+-----+------------------+-------+
| one| 1.09:39:26| 0|
| two|1.09:39:26.1234567| 0|
|three| 09:39:26.1234567| 34766|
+-----+------------------+-------+
推荐阅读
- php - 是否可以在 PHP 中为 setcookie() 定义两个 URL?
- c++ - 是否有一个大小为 N 的 c++ 标准库容器具有 log(N) 插入和搜索但有 N 次迭代而不是 N*log(N)?
- java - 试图意图一个浮点数组android
- c++ - 我该如何解决这个奇怪的 c++ std map 问题?
- java - Selenium 4.0(Java) + Cucumber + Browserstack + Applitools
- php - 使用 httpd 时不加载 PDO 扩展,但使用内置服务器时显示
- laravel - htaccess 误导。它重复文件名
- linux - 仅调整大于 250px 的 gif 大小
- java - Maven无需配置即可运行不同的类
- html - 如何将文本向左移动