首页 > 解决方案 > Pyspark: TypeError: int is required...得到类型列

问题描述

我正在使用嵌套的 Json 结构。我创建了一个数据框并通过执行以下操作添加了一个列:

jsonDf = jsonDf.withColumn("REPORT_TIMESTAMP", to_timestamp(jsonDF.reportData.timestamp))/

一切都很好,直到这里。接下来我需要做的是从“REPORT_TIMESTAMP”中得出年份。我尝试了各种方法,例如:

jsonDf.withColumn("YEAR", datetime.fromtimestamp(to_timestamp(jsonDF.reportData.timestamp).cast("integer"))

以“TypeError:需要一个整数(获取类型列)

我也试过:

jsonDf.withColumn("YEAR", datetime.date.to_timestamp(jsonDF.reportData.timestamp).year)

这给了我“AttributeError:'method_descriptor'对象没有属性'to_timestamp'

谁能更正我以前的两种方法,使其有效,甚至建议我还没有考虑到的另一种选择?非常感谢提前

标签: pythonjsondataframepysparknested

解决方案


您正在混合 Python 函数datetime.date.to_timestampPySpark 函数

就这么简单.withColumn('YEAR', F.year('dt'))


推荐阅读