使用pyspark将spark的dataframe转化为pandas的dataframe时,如果frame中"date"格式的数据,会报错:
AttributeError: Can only use .dt accessor with datetimelike values
验证数据确实是"date“格式的数据:
原因&解决方案
pyspark的datetime格式无法与pandas的时间类型格式datetime直接对应。
需要转化为pyspark的timestamp格式:
from pyspark.sql.functions import to_timestamp
for f in [f[0] for f in ret2.dtypes if f[1]=='date']:
ret = ret.withColumn(f, F.to_timestamp(F.col(f), 'yyyy-MM-dd'))