Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PySpark中创建年、月和日的日期?_Python_Apache Spark_Pyspark_Apache Spark Sql_Pyspark Dataframes - Fatal编程技术网

Python 如何在PySpark中创建年、月和日的日期?

Python 如何在PySpark中创建年、月和日的日期?,python,apache-spark,pyspark,apache-spark-sql,pyspark-dataframes,Python,Apache Spark,Pyspark,Apache Spark Sql,Pyspark Dataframes,我有三个关于年、月和日的专栏。如何使用这些属性在PySpark中创建日期?您可以使用concat_ws()将列与-合并转换为日期 #sampledata df.show() #+----+-----+---+ #|year|month|day| #+----+-----+---+ #|2020| 12| 12| #+----+-----+---+ from pyspark.sql.functions import * df.withColumn("date",concat_ws("-",

我有三个关于年、月和日的专栏。如何使用这些属性在PySpark中创建日期?

您可以使用
concat_ws()
将列与
-
合并转换为日期

#sampledata
df.show()

#+----+-----+---+
#|year|month|day|
#+----+-----+---+
#|2020|   12| 12|
#+----+-----+---+
from pyspark.sql.functions import *

df.withColumn("date",concat_ws("-",col("year"),col("month"),col("day")).cast("date")).show()
+----+-----+---+----------+
|year|month|day|      date|
+----+-----+---+----------+
|2020|   12| 12|2020-12-12|
+----+-----+---+----------+

#dynamic way
cols=["year","month","day"]
df.withColumn("date",concat_ws("-",*cols).cast("date")).show()
#+----+-----+---+----------+
#|year|month|day|      date|
#+----+-----+---+----------+
#|2020|   12| 12|2020-12-12|
#+----+-----+---+----------+

#using date_format,to_timestamp,from_unixtime(unix_timestamp) functions

df.withColumn("date",date_format(concat_ws("-",*cols),"yyyy-MM-dd").cast("date")).show()
df.withColumn("date",to_timestamp(concat_ws("-",*cols),"yyyy-MM-dd").cast("date")).show()
df.withColumn("date",to_date(concat_ws("-",*cols),"yyyy-MM-dd")).show()
df.withColumn("date",from_unixtime(unix_timestamp(concat_ws("-",*cols),"yyyy-MM-dd"),"yyyy-MM-dd").cast("date")).show()
#+----+-----+---+----------+
#|year|month|day|      date|
#+----+-----+---+----------+
#|2020|   12| 12|2020-12-12|
#+----+-----+---+----------+
对于Spark 3+,您可以使用以下功能:

df = df.withColumn("date", expr("make_date(year, month, day)"))

在DataBrick上使用pyspark,当您有一个纯字符串时,这里有一个解决方案;不幸的是,unix_时间戳可能无法工作,并产生错误的结果。在pyspark中使用unix\u timestamp或to\u date命令时,可能会引起很大的麻烦。 例如,如果您的字符串有一个fromat,如“20140625”,那么它们只会生成完全错误的输入日期版本。在我的例子中,除了再次构建字符串并将其转换为如下日期的concatantion之外,没有其他方法可以工作

from pyspark.sql.functions import col, lit, substring, concat

# string format to deal with: "20050627","19900401",...

#Create a new column with a shorter name to keep the originalcolumns as well
df.withColumn("dod",col("date_of_death"))

#create date upon string components
df.withColumn("dod", concat(substring(df.dod,1,4),lit("-"),substring(df.dod,5,2),lit("-"),substring(df.dod,7,2)).cast("date")))
结果如下所示:

注意使用以下格式。它很可能会奇怪地产生错误的结果,而不会引发和显示任何错误。就我而言,它毁了我大部分的分析:

### wrong use! use only on strings with delimeters ("yyyy-mm-dd) and be highly causious!
f.to_date(f.unix_timestamp(df.dod,"yyyymmdd").cast("timestamp"))
所举的例子(2020-12-12)非常简单。但是,在年=2020、月=1、日=1的情况下,它是如何工作的呢?