【问题标题】:Pyspark column: convert string to datetypePyspark 列:将字符串转换为日期类型
【发布时间】:2020-07-25 14:27:23
【问题描述】:

我正在尝试将字符串类型的 pyspark 列转换为日期类型,如下所示。

**Date**
31 Mar 2020
2  Apr 2020
29 Jan 2019
8  Sep 2109

需要输出:

31-03-2020
02-04-2020
29-01-2019
08-04-2109  

谢谢。

【问题讨论】:

  • 基本上,我想将年、月和日期提取为单独的列。谢谢。

标签: python-3.x dataframe pyspark


【解决方案1】:

对于这种情况,您可以在内置函数中使用dayofmonth,year,month(或)date_format()(或)from_unixtime(unix_timestamp())

Example:

#sample data
df=spark.createDataFrame([("31 Mar 2020",),("2 Apr 2020",),("29 Jan 2019",)],["Date"])
#DataFrame[Date: string]
df.show()
#+-----------+
#|       Date|
#+-----------+
#|31 Mar 2020|
#| 2 Apr 2020|
#|29 Jan 2019|
#+-----------+

from pyspark.sql.functions import *

df.withColumn("new_dt", to_date(col("Date"),"dd MMM yyyy")).\
withColumn("year",year(col("new_dt"))).\
withColumn("month",month(col("new_dt"))).\
withColumn("day",dayofmonth(col("new_dt"))).\
show()

#+-----------+----------+----+-----+---+
#|       Date|    new_dt|year|month|day|
#+-----------+----------+----+-----+---+
#|31 Mar 2020|2020-03-31|2020|    3| 31|
#| 2 Apr 2020|2020-04-02|2020|    4|  2|
#|29 Jan 2019|2019-01-29|2019|    1| 29|
#+-----------+----------+----+-----+---+

#using date_format
df.withColumn("new_dt", to_date(col("Date"),"dd MMM yyyy")).\
withColumn("year",date_format(col("new_dt"),"yyyy")).\
withColumn("month",date_format(col("new_dt"),"MM")).\
withColumn("day",date_format(col("new_dt"),"dd")).show()

#+-----------+----------+----+-----+---+
#|       Date|    new_dt|year|month|day|
#+-----------+----------+----+-----+---+
#|31 Mar 2020|2020-03-31|2020|   03| 31|
#| 2 Apr 2020|2020-04-02|2020|   04| 02|
#|29 Jan 2019|2019-01-29|2019|   01| 29|
#+-----------+----------+----+-----+---+

【讨论】:

  • 谢谢。我试过你的第一个选项。我收到错误为“ TypeError:'str' object is not callable”
  • @Lilly,你能添加你的数据框架构吗?
  • “日期”列是字符串类型。谢谢。
  • @Lilly,我添加了创建数据框步骤,日期为 string 类型,代码也适用于字符串类型!
  • @Shu ,我们可以将日期格式转换回 %B%y 格式吗?示例结果为“Jan-21”
【解决方案2】:

to_date 函数需要天数为 02' 2' 而不是 2强>。因此,我们可以使用 regex 来去除空格,那么只要字符串的 lengthless than max(9),我们可以添加字符串的0 to the start。然后我们可以应用 to_date 并使用它来提取您的其他列(日、月、年)。也可以使用 date_format指定格式保存您的日期。

df.show()#sample df
+-----------+
|       Date|
+-----------+
|31 Mar 2020|
|2  Apr 2020|
|29 Jan 2019|
|8  Sep 2019|
+-----------+

from pyspark.sql import functions as F    
df.withColumn("regex", F.regexp_replace("Date","\ ",""))\
  .withColumn("Date", F.when(F.length("regex")<9, F.concat(F.lit(0),F.col("regex")))\
              .otherwise(F.col("regex"))).drop("regex")\
  .withColumn("Date", F.to_date("Date",'ddMMMyyyy'))\
  .withColumn("Year", F.year("Date"))\
  .withColumn("Month",F.month("Date"))\
  .withColumn("Day", F.dayofmonth("Date"))\
  .withColumn("Date_Format2", F.date_format("Date", 'dd-MM-yyyy'))\
  .show()

#output
+----------+----+-----+---+------------+
|      Date|Year|Month|Day|Date_Format2|
+----------+----+-----+---+------------+
|2020-03-31|2020|    3| 31|  31-03-2020|
|2020-04-02|2020|    4|  2|  02-04-2020|
|2019-01-29|2019|    1| 29|  29-01-2019|
|2019-09-08|2019|    9|  8|  08-09-2019|
+----------+----+-----+---+------------+

【讨论】:

    猜你喜欢
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2019-10-27
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多