我正在使用嵌套的 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: an integer is required (got type Column)”結尾
我也試過:
jsonDf.withColumn("YEAR", datetime.date.to_timestamp(jsonDF.reportData.timestamp).year)
這給了我“AttributeError:'method_descriptor'物件沒有屬性'to_timestamp'
任何人都可以糾正我之前的兩種方法,使其有效,或者甚至建議我還沒有出現在雷達上的另一種選擇嗎?非常感謝提前
uj5u.com熱心網友回復:
您在 Python 函式datetime.date.to_timestamp和PySpark 函式之間混用。
就這么簡單 .withColumn('YEAR', F.year('dt'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333870.html
