我在字串資料型別的資料框中有一個日期,格式dd/MM/yyyy如下:

當我嘗試將字串轉換為日期格式時,所有函式都回傳空值。
希望將資料型別轉換為DateType.
uj5u.com熱心網友回復:
看起來您的日期字串包含引號,您需要regexp_replace在呼叫之前洗掉它們,例如使用to_date:
import pyspark.sql.functions as F
df = spark.createDataFrame([("'31-12-2021'",), ("'30-11-2021'",), ("'01-01-2022'",)], ["Birth_Date"])
df = df.withColumn(
"Birth_Date",
F.to_date(F.regexp_replace("Birth_Date", "'", ""), "dd-MM-yyyy")
)
df.show()
# ----------
#|Birth_Date|
# ----------
#|2021-12-31|
#|2021-11-30|
#|2022-01-01|
# ----------
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/402726.html
標籤:
