3 回答

TA貢獻1943條經驗 獲得超7個贊
我終于能夠解決這個問題。
使用UDF:
def convert_date(x):
mDt = datetime.datetime(1899, 12, 30)
dlt = mDt + datetime.timedelta(days=x)
return dlt.strftime("%Y-%m-%d")
convert_date_udf = udf(lambda z: convert_date(z), StringType())
df = df.withColumn('hire date', convert_date_udf('hire date').alias('hire date new'))
不使用UDF:
df = df.withColumn('hire date', F.expr("date_add(to_date('1899-12-30'), cast(`hire date` as int))").cast(StringType())
希望能幫助到你!

TA貢獻1864條經驗 獲得超6個贊
我想有更優雅的方法可以做到這一點,但這就是我現在想出的。
from datetime import date
df.hire_date = df.hire_date.apply(date.fromordinal) # this will give you date in dash format
df.hire_date = df.hire_date.apply(lambda x: str(x).replace('-', '/')) # this will simply replace dash with slash
希望這對您有用:)
添加回答
舉報