pyspark to_date 无法推断格式
pyspark to_date fail to infer format
我有一个字符串类型的列,其中值的形式为 'Jun 2019'; 'Sep 2020';等等
我试图从中提取年份,但似乎 to_date 函数无法将数据转换为日期时间格式
这里是试过的代码
df = df.withColumn('year_launch', year(to_date(df.launch)))
df.show()
当前结果
您必须将日期格式传递给 to_date
函数。
from pyspark.sql import functions as F
df.withColumn('year_launch', F.year(F.to_date("launch", 'MMM yyyy'))).show()
输出:
+--------+-----------+
| launch|year_launch|
+--------+-----------+
|Jun 2019| 2019|
|Sep 2020| 2020|
|Jun 2021| 2021|
|Oct 2021| 2021|
+--------+-----------+
我有一个字符串类型的列,其中值的形式为 'Jun 2019'; 'Sep 2020';等等 我试图从中提取年份,但似乎 to_date 函数无法将数据转换为日期时间格式
这里是试过的代码
df = df.withColumn('year_launch', year(to_date(df.launch)))
df.show()
当前结果
您必须将日期格式传递给 to_date
函数。
from pyspark.sql import functions as F
df.withColumn('year_launch', F.year(F.to_date("launch", 'MMM yyyy'))).show()
输出:
+--------+-----------+
| launch|year_launch|
+--------+-----------+
|Jun 2019| 2019|
|Sep 2020| 2020|
|Jun 2021| 2021|
|Oct 2021| 2021|
+--------+-----------+