在胶水中更改日期格式
change date format in glue
我有一个看起来像这样的数据集。我想将日期列中的所有值更改为正确的日期时间格式。 dd-mm-2020(如果可能,最好还根据日期按升序排列所有行)。我怎样才能在 spark 中实现这一点?
Name |Type | date |Value |
ALZA CZ|New | 01/01(FRI) | 0
CLPA CZ|New | 01/01(FRI) | 1
ALZA CZ|Old | 01/02(SAT) | 1
CLPA CZ|Old | 01/02(SAT) | 5
数据源已转换为数据帧:
dataframe = datasource0.toDF()
首先,您可以使用 regexp_replace
(Python docs here) 更改 Date
列的字符串,它有 3 个参数:列操作,你要匹配的正则表达式,你要替换匹配文本的内容。
日期升序排序需要将Date
列转换为DateType
。为此,您可以使用 to_date
方法 (Python docs here). However, this will inevitably change your date format to YYYY-MM-DD
("that's because, as you'd guess, in ascending order we first look at sorting by year, then by month, and lastly by day to have 2019 come before 2020, then January come before February, and the 1st of the month to come before its 2nd). To handle this, we simply use the date_format
method (Python docs here) 在按日期 对行进行排序后指定我们想要的日期格式想放出来。
为了测试这一点,我在您的 dataframe
:
中添加了一些行
+-------+----+----------+-----+
| Name|Type| Date|Value|
+-------+----+----------+-----+
|ALZA CZ| New|01/01(FRI)| 0|
|CLPA CZ| New|01/01(FRI)| 1|
|YYYY YY| Old|01/29(FRI)| 1|
|ALZA CZ| Old|01/02(SAT)| 1|
|XXXX XX| New|03/12(SAT)| 5|
|CLPA CZ| Old|01/02(SAT)| 5|
+-------+----+----------+-----+
然后我们使用上面的方法将Date
转换为我们需要的。这里我分两步进行正则表达式匹配,一是将月份和日期之间的/
替换为-
,一是将括号中的文本替换为-2020
。接下来,我只是将 Date
转换为 DateType
列(通过指定当前的 MM-dd-yyyy
日期格式)并相应地对 DataFrame 行进行排序,然后最后一次将 Date
转换为所需的 dd-MM-yyyy
字符串格式。
// In Python
dataframe.withColumn("Date", regexp_replace("Date", "/", "-"))
.withColumn("Date", regexp_replace("Date", "\([a-z]+\)", "-2020"))
.withColumn("Date", to_date("Date", "MM-dd-yyyy"))
.orderBy("Date")
.withColumn("Date", date_format("Date", "dd-MM-yyyy"))
// In Scala
dataframe.withColumn("Date", regexp_replace(col("Date"), "/", "-"))
.withColumn("Date", regexp_replace(col("Date"), "\([A-Z]+\)", "-2020"))
.withColumn("Date", to_date(col("Date"), "MM-dd-yyyy"))
.orderBy("Date")
.withColumn("Date", date_format(col("Date"), "dd-MM-yyyy"))
被操纵的 dataframe
现在看起来像这样:
+-------+----+----------+-----+
| Name|Type| Date|Value|
+-------+----+----------+-----+
|CLPA CZ| New|01-01-2020| 1|
|ALZA CZ| New|01-01-2020| 0|
|ALZA CZ| Old|02-01-2020| 1|
|CLPA CZ| Old|02-01-2020| 5|
|YYYY YY| Old|29-01-2020| 1|
|XXXX XX| New|12-03-2020| 5|
+-------+----+----------+-----+
我有一个看起来像这样的数据集。我想将日期列中的所有值更改为正确的日期时间格式。 dd-mm-2020(如果可能,最好还根据日期按升序排列所有行)。我怎样才能在 spark 中实现这一点?
Name |Type | date |Value |
ALZA CZ|New | 01/01(FRI) | 0
CLPA CZ|New | 01/01(FRI) | 1
ALZA CZ|Old | 01/02(SAT) | 1
CLPA CZ|Old | 01/02(SAT) | 5
数据源已转换为数据帧:
dataframe = datasource0.toDF()
首先,您可以使用 regexp_replace
(Python docs here) 更改 Date
列的字符串,它有 3 个参数:列操作,你要匹配的正则表达式,你要替换匹配文本的内容。
日期升序排序需要将Date
列转换为DateType
。为此,您可以使用 to_date
方法 (Python docs here). However, this will inevitably change your date format to YYYY-MM-DD
("that's because, as you'd guess, in ascending order we first look at sorting by year, then by month, and lastly by day to have 2019 come before 2020, then January come before February, and the 1st of the month to come before its 2nd). To handle this, we simply use the date_format
method (Python docs here) 在按日期 对行进行排序后指定我们想要的日期格式想放出来。
为了测试这一点,我在您的 dataframe
:
+-------+----+----------+-----+
| Name|Type| Date|Value|
+-------+----+----------+-----+
|ALZA CZ| New|01/01(FRI)| 0|
|CLPA CZ| New|01/01(FRI)| 1|
|YYYY YY| Old|01/29(FRI)| 1|
|ALZA CZ| Old|01/02(SAT)| 1|
|XXXX XX| New|03/12(SAT)| 5|
|CLPA CZ| Old|01/02(SAT)| 5|
+-------+----+----------+-----+
然后我们使用上面的方法将Date
转换为我们需要的。这里我分两步进行正则表达式匹配,一是将月份和日期之间的/
替换为-
,一是将括号中的文本替换为-2020
。接下来,我只是将 Date
转换为 DateType
列(通过指定当前的 MM-dd-yyyy
日期格式)并相应地对 DataFrame 行进行排序,然后最后一次将 Date
转换为所需的 dd-MM-yyyy
字符串格式。
// In Python
dataframe.withColumn("Date", regexp_replace("Date", "/", "-"))
.withColumn("Date", regexp_replace("Date", "\([a-z]+\)", "-2020"))
.withColumn("Date", to_date("Date", "MM-dd-yyyy"))
.orderBy("Date")
.withColumn("Date", date_format("Date", "dd-MM-yyyy"))
// In Scala
dataframe.withColumn("Date", regexp_replace(col("Date"), "/", "-"))
.withColumn("Date", regexp_replace(col("Date"), "\([A-Z]+\)", "-2020"))
.withColumn("Date", to_date(col("Date"), "MM-dd-yyyy"))
.orderBy("Date")
.withColumn("Date", date_format(col("Date"), "dd-MM-yyyy"))
被操纵的 dataframe
现在看起来像这样:
+-------+----+----------+-----+
| Name|Type| Date|Value|
+-------+----+----------+-----+
|CLPA CZ| New|01-01-2020| 1|
|ALZA CZ| New|01-01-2020| 0|
|ALZA CZ| Old|02-01-2020| 1|
|CLPA CZ| Old|02-01-2020| 5|
|YYYY YY| Old|29-01-2020| 1|
|XXXX XX| New|12-03-2020| 5|
+-------+----+----------+-----+