pyspark - 在 PySpark 中将整数列转换为日期
问题描述
我有一个birth_date
以这种格式调用的整数列:20141130
我想将其转换为2014-11-30
PySpark。
这会错误地转换日期:
.withColumn("birth_date", F.to_date(F.from_unixtime(F.col("birth_date"))))
这给出了一个错误:argument 1 requires (string or date or timestamp) type, however, 'birth_date' is of int type
.withColumn('birth_date', F.to_date(F.unix_timestamp(F.col('birth_date'), 'yyyyMMdd').cast('timestamp')))
将其转换为我想要的日期的最佳方法是什么?
解决方案
在将birth_date
列从传递Integer
给函数String
之前将其转换为:to_date
from pyspark.sql import functions as F
df.withColumn("birth_date", F.to_date(F.col("birth_date").cast("string"), \
'yyyyMMdd')).show()
+----------+
|birth_date|
+----------+
|2014-11-30|
+----------+
推荐阅读
- email - RFC 2822 格式 - 是否包含附件
- ios - tableView(_:heightForHeaderInSection:) 不工作
- haskell - `Functor`不是`Category`的超类是否有原因?
- ionic-framework - 我想在离子选择中选择离子选项后添加离子项目
- xml - 水壶(PDI)8.1读取xml错误
- scala - 在循环 DataFrame 并访问外部范围变量时获取 NullPointerException
- php - 没有这样的主机是已知的
- ibm-watson - Watson 助手中的逻辑确定对话
- c# - 带有 IsBackground 的 Lambda 线程
- python - @property 在 Python 中私有化变量