在pyspark中将yyyymmdd转换为MM-dd-yyyy格式

时间:2016-12-30 07:19:40

标签: apache-spark pyspark apache-spark-sql pyspark-sql

我有一个大型数据框df,其中包含yyyymmdd格式的日期列,如何在pySpark中将其转换为MM-dd-yyyy

2 个答案:

答案 0 :(得分:2)

from datetime import datetime
from pyspark.sql.functions import col,udf
from pyspark.sql.types import DateType


rdd = sc.parallelize(['20161231', '20140102', '20151201', '20161124'])
df1 = sqlContext.createDataFrame(rdd, ['old_col'])

//UDF to convert string to date
func =  udf (lambda x: datetime.strptime(x, '%Y%M%d'), DateType())

df = df1.withColumn('new_col', date_format(func(col('old_col')), 'MM-dd-yyy'))

df.show()

答案 1 :(得分:0)

这也有效:

from datetime import datetime
from pyspark.sql.functions import col,udf,unix_timestamp
from pyspark.sql.types import DateType


func =  udf(lambda x: datetime.strptime(str(x), '%m%d%y'), DateType())

df2 = df.withColumn('date', func(col('InvcDate')))