如何查找您的列中是否有日期值
How to find if there is a Date Value in your column
我有一个要求,我需要在我的列值中找到日期。
该专栏有大约 300 万条记录,由特殊字符、数字、文本和日期组成。我可以做些什么来找到 Date 函数并通过 Teradata Column 将其删除。
正则表达式功能也有帮助。
我不知道日期函数是什么格式。
鉴于您不知道数据的内容,您的工作是探索性的,这意味着您的第一步是查询数据。寻找您能想到的尽可能多的格式的日期。你会摆脱最常见的。我以我在 R 中编写的方法开始了你的学习。你可以用你选择的语言做类似的事情。
您永远不会知道有多少日期是您没有想到的格式。但是,您可以尝试一些开放式搜索,这些搜索可能会剔除其中的一些,甚至匹配您希望看到但不符合任何格式的有缺陷的数据。我包括了一个搜索,它只在搜索字符串中的任何位置查找月份缩写。
为了制作这个演示,每次添加新的 RegEx 进行测试时,我都必须剪切并粘贴到测试用例中。编写测试床将是一个令人满意的策略。这可以是一个 table 或向量来保存您要执行的每个 RegEx 模式,然后是一个简单的循环来遍历 运行 它们。
这是我的 R 代码。
library(stringr)
# Exploratory date patterns.
# 04/04/2020
fmt1 <- "\d{2}/\d{2}/\d{4}"
# 4/4/2020
fmt2 <- "\d{1,2}/\d{1,2}/\d{4}"
# Apr. 1, 2020
# If I have a large list, I like building it like this so the part I maintain
# is human readable. You can implement this kind of approach in whatever
# language you use.
month_3ltr <- c("Jan", "Feb", "Mar", "Apr", "May")
month_pat <- paste(month_3ltr, "\.", sep = "")
month_pat <- paste(month_pat, collapse = "|")
fmt3 <- str_c("^(", month_pat, ")\s\d{1,2},\s\d{4}")
# Review pattern
fmt3
fmt4 <- str_c(month_pat) # Find anything that just mentions a month.
# Keep going, as many formats as you can think of.
# Test data
str1 <- "04/01/2020"
str2 <- "4/1/2020"
str3 <- "Apr. 1, 2020"
str4 <- "Defective data: Apr. First, 2020"
str5 <- "Fake news"
# Test cases
# Format 1
str_extract(str1, fmt1)
str_extract(str2, fmt1)
str_extract(str3, fmt1)
str_extract(str4, fmt1)
str_extract(str5, fmt1)
# Format 2
str_extract(str1, fmt2)
str_extract(str2, fmt2)
str_extract(str3, fmt2)
str_extract(str4, fmt2)
str_extract(str5, fmt2)
# Format 3
str_extract(str1, fmt3)
str_extract(str2, fmt3)
str_extract(str3, fmt3)
str_extract(str4, fmt3)
str_extract(str5, fmt3)
# Format 4
str_extract(str1, fmt4)
str_extract(str2, fmt4)
str_extract(str3, fmt4)
str_extract(str4, fmt4)
str_extract(str5, fmt4)
我的结果:
> # Test cases
> # Format 1
> str_extract(str1, fmt1)
[1] "04/01/2020"
> str_extract(str2, fmt1)
[1] NA
> str_extract(str3, fmt1)
[1] NA
> str_extract(str4, fmt1)
[1] NA
> str_extract(str5, fmt1)
[1] NA
>
> # Format 2
> str_extract(str1, fmt2)
[1] "04/01/2020"
> str_extract(str2, fmt2)
[1] "4/1/2020"
> str_extract(str3, fmt2)
[1] NA
> str_extract(str4, fmt2)
[1] NA
> str_extract(str5, fmt2)
[1] NA
>
> # Format 3
> str_extract(str1, fmt3)
[1] NA
> str_extract(str2, fmt3)
[1] NA
> str_extract(str3, fmt3)
[1] "Apr. 1, 2020"
> str_extract(str4, fmt3)
[1] NA
> str_extract(str5, fmt3)
[1] NA
>
> # Format 4
> str_extract(str1, fmt4)
[1] NA
> str_extract(str2, fmt4)
[1] NA
> str_extract(str3, fmt4)
[1] "Apr."
> str_extract(str4, fmt4)
[1] "Apr."
> str_extract(str5, fmt4)
[1] NA
我有一个要求,我需要在我的列值中找到日期。
该专栏有大约 300 万条记录,由特殊字符、数字、文本和日期组成。我可以做些什么来找到 Date 函数并通过 Teradata Column 将其删除。 正则表达式功能也有帮助。
我不知道日期函数是什么格式。
鉴于您不知道数据的内容,您的工作是探索性的,这意味着您的第一步是查询数据。寻找您能想到的尽可能多的格式的日期。你会摆脱最常见的。我以我在 R 中编写的方法开始了你的学习。你可以用你选择的语言做类似的事情。
您永远不会知道有多少日期是您没有想到的格式。但是,您可以尝试一些开放式搜索,这些搜索可能会剔除其中的一些,甚至匹配您希望看到但不符合任何格式的有缺陷的数据。我包括了一个搜索,它只在搜索字符串中的任何位置查找月份缩写。
为了制作这个演示,每次添加新的 RegEx 进行测试时,我都必须剪切并粘贴到测试用例中。编写测试床将是一个令人满意的策略。这可以是一个 table 或向量来保存您要执行的每个 RegEx 模式,然后是一个简单的循环来遍历 运行 它们。
这是我的 R 代码。
library(stringr)
# Exploratory date patterns.
# 04/04/2020
fmt1 <- "\d{2}/\d{2}/\d{4}"
# 4/4/2020
fmt2 <- "\d{1,2}/\d{1,2}/\d{4}"
# Apr. 1, 2020
# If I have a large list, I like building it like this so the part I maintain
# is human readable. You can implement this kind of approach in whatever
# language you use.
month_3ltr <- c("Jan", "Feb", "Mar", "Apr", "May")
month_pat <- paste(month_3ltr, "\.", sep = "")
month_pat <- paste(month_pat, collapse = "|")
fmt3 <- str_c("^(", month_pat, ")\s\d{1,2},\s\d{4}")
# Review pattern
fmt3
fmt4 <- str_c(month_pat) # Find anything that just mentions a month.
# Keep going, as many formats as you can think of.
# Test data
str1 <- "04/01/2020"
str2 <- "4/1/2020"
str3 <- "Apr. 1, 2020"
str4 <- "Defective data: Apr. First, 2020"
str5 <- "Fake news"
# Test cases
# Format 1
str_extract(str1, fmt1)
str_extract(str2, fmt1)
str_extract(str3, fmt1)
str_extract(str4, fmt1)
str_extract(str5, fmt1)
# Format 2
str_extract(str1, fmt2)
str_extract(str2, fmt2)
str_extract(str3, fmt2)
str_extract(str4, fmt2)
str_extract(str5, fmt2)
# Format 3
str_extract(str1, fmt3)
str_extract(str2, fmt3)
str_extract(str3, fmt3)
str_extract(str4, fmt3)
str_extract(str5, fmt3)
# Format 4
str_extract(str1, fmt4)
str_extract(str2, fmt4)
str_extract(str3, fmt4)
str_extract(str4, fmt4)
str_extract(str5, fmt4)
我的结果:
> # Test cases
> # Format 1
> str_extract(str1, fmt1)
[1] "04/01/2020"
> str_extract(str2, fmt1)
[1] NA
> str_extract(str3, fmt1)
[1] NA
> str_extract(str4, fmt1)
[1] NA
> str_extract(str5, fmt1)
[1] NA
>
> # Format 2
> str_extract(str1, fmt2)
[1] "04/01/2020"
> str_extract(str2, fmt2)
[1] "4/1/2020"
> str_extract(str3, fmt2)
[1] NA
> str_extract(str4, fmt2)
[1] NA
> str_extract(str5, fmt2)
[1] NA
>
> # Format 3
> str_extract(str1, fmt3)
[1] NA
> str_extract(str2, fmt3)
[1] NA
> str_extract(str3, fmt3)
[1] "Apr. 1, 2020"
> str_extract(str4, fmt3)
[1] NA
> str_extract(str5, fmt3)
[1] NA
>
> # Format 4
> str_extract(str1, fmt4)
[1] NA
> str_extract(str2, fmt4)
[1] NA
> str_extract(str3, fmt4)
[1] "Apr."
> str_extract(str4, fmt4)
[1] "Apr."
> str_extract(str5, fmt4)
[1] NA