将字符串 dd.mm.yyyy 转换为日期格式 yyyy-MM-dd,使用 Pyspark。

huangapple go评论54阅读模式
英文:

Converting string dd.mm.yyyy to date format yyyy-MM-dd using Pyspark

问题

我有一列日期,格式为字符串:dd.mm.yyyy,我想使用Pyspark将其转换为日期格式yyyy-MM-dd,我尝试了以下方法,但返回了空值

df.withColumn("date_col", to_date("string_col", "yyyy.MM.dd")
string_col date_col
02.11.2008 2008-11-02
26.02.2021 2021-02-26
英文:

I have a column with date in string format: dd.mm.yyyy I want to convert it into date format yyyy-MM-dd using Pyspark, I have tried the following but it's returning null values

df.withColumn("date_col", to_date("string_col", "yyyy-mmm-dd")
string_col date_col
02.11.2008 null
26.02.2021 null

答案1

得分: 0

请确保将格式参数与字符串中的正确格式匹配。例如,df.withColumn("date_col", to_date("string_col", "dd.mm.yyyy")

确保日期、月份和年份的放置是正确的,以及分隔符'.'而不是'-'。

还可以参考文档

英文:

You should match the format argument with the right format in your string.
E.g. df.withColumn("date_col", to_date("string_col", "dd.mm.yyyy")

Make sure the placement of the day, month and year is correct, as well as the seperator '.' instead of '-'.

See also the docs

huangapple
  • 本文由 发表于 2023年2月8日 20:32:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定