Date pyspark-生成日期序列
我正在尝试生成一个日期序列Date pyspark-生成日期序列,date,pyspark,sequence,aws-glue,Date,Pyspark,Sequence,Aws Glue,我正在尝试生成一个日期序列 df1 = df.withColumn("start_dt" ,F.to_date(F.col("start_date"),"yyyy-mm-dd")).withColumn("end_dt", F.to_date(F.col("end_date"),"yyyy-mm-dd")) df1.select("start_dt","end_dt").show() print("type(start_dt) ",type("start_dt")) print("type(e
df1 = df.withColumn("start_dt" ,F.to_date(F.col("start_date"),"yyyy-mm-dd")).withColumn("end_dt", F.to_date(F.col("end_date"),"yyyy-mm-dd"))
df1.select("start_dt","end_dt").show()
print("type(start_dt) ",type("start_dt"))
print("type(end_dt) " ,type("end_dt"))
df2 =df1.withColumn("lineoffdate", F.expr("""sequence(start_dt,end_dt,1)"""))
**下面是输出
+----+----------+----------+
| start_date | end_date|
+----+----------+----------+
| 2020-02-01 |2020-03-21|
+-------------------+------+
type(start_dt) <class 'str'>
type(end_dt) <class 'str'>
+----+----------+----------+
|开始日期|结束日期|
+----+----------+----------+
| 2020-02-01 |2020-03-21|
+-------------------+------+
类型(开始时间)
类型(结束时)
由于数据类型不匹配,无法解析“序列(start_dt
,end_dt
,1)”:序列仅支持整数、时间戳或日期类型;第1行位置0强>
即使将开始日期和结束日期转换为日期或时间戳,我仍然可以看到列的类型,并且在生成日期序列时出现上述错误。您说它应该与
date
或timestamp
(日历类型)一起工作是正确的,你犯的唯一错误是你把步骤放在序列中作为整数
,而它应该是日历间隔(比如间隔1天
):
我试着打印模式|--start_dt:date(nullable=true)|--end_dt:date(nullable=true)不明白为什么序列不起作用你真聪明。非常感谢!
df.withColumn("start_date",F.to_date("start_date")).withColumn("end_date", F.to_date("end_date"))\
.withColumn("lineofdate", F.expr("""sequence(start_date,end_date,interval 1 day)""")).show()
+----------+----------+--------------------+
|start_date| end_date| lineofdate|
+----------+----------+--------------------+
|2020-02-01|2020-03-21|[2020-02-01, 2020...|
+----------+----------+--------------------+