如何在pyspark中将日期时间从字符串格式转换为日期时间格式? [英] How to convert datetime from string format into datetime format in pyspark?

查看:831
本文介绍了如何在pyspark中将日期时间从字符串格式转换为日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sqlContext创建了一个数据框,并且日期时间格式存在问题,因为它被标识为字符串。

I created a dataframe using sqlContext and I have a problem with the datetime format as it is identified as string.

df2 = sqlContext.createDataFrame(i[1])
df2.show
df2.printSchema()

结果:

2016-07-05T17:42:55.238544+0900
2016-07-05T17:17:38.842567+0900
2016-06-16T19:54:09.546626+0900
2016-07-05T17:27:29.227750+0900
2016-07-05T18:44:12.319332+0900

string (nullable = true)

由于datetime模式为一个字符串,我想将其更改为日期时间格式,如下所示:

Since the datetime schema is a string, I want to change it to datetime format as follows:

df3 =  df2.withColumn('_1', df2['_1'].cast(datetime()))

这里出现错误:
TypeError:找不到必需的参数 year(位置1)

Here I got an error: TypeError: Required argument 'year' (pos 1) not found

我该怎么解决这个问题?

What should I do to solve this problem?

推荐答案

尝试一下:

from pyspark.sql.types import DateType
ndf = df2.withColumn('_1', df2['_1'].cast(DateType()))

这篇关于如何在pyspark中将日期时间从字符串格式转换为日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆