python - 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'
谁能更正我以前的两种方法,使其有效,甚至建议我还没有考虑到的另一种选择?非常感谢提前
解决方案
您正在混合 Python 函数datetime.date.to_timestamp
和PySpark 函数。
就这么简单.withColumn('YEAR', F.year('dt'))
推荐阅读
- pandas - 根据另一个 df 中的特定索引替换一个 df 中的值
- flutter - Flutter:如何防止 BottomNavigationBar 在选项卡更改或推送到其他屏幕时重建?
- reactjs - Axios 发布请求在 React Native 中不起作用
- javascript - 过滤掉递归目录路径
- c# - .Net Core 会话在某些控制器方法上返回 null
- python-3.x - Python:获取两个文件之间的唯一行
- c# - 泛型类不能将类型“T”隐式转换为“Int”
- python - 当神经网络无法映射数据之间的关系时,可能是什么原因?
- hyperledger-fabric - 如何获取历史状态的传递给事务的参数
- bash - Bash 将所有参数从函数传递到命令,除了最后一个