Pyspark PickleException:构造ClassDict(对于numpy.dtype)需要零个参数

Pyspark PickleException:构造ClassDict(对于numpy.dtype)需要零个参数,pyspark,user-defined-functions,databricks,Pyspark,User Defined Functions,Databricks,我不明白如何解决这个问题,我已经讨论了这里的一些问题,但没有找到一个完美的答案 我有一个数据框,它有以下重要的列:building_id、area、height 我试图编写的UDF计算面积的平方根和高度之间的差值。它返回一个值,该值应添加到数据帧中 def calculate_difference(area, height): # calculate the square root of the area import numpy as np nr = np.sqrt(area)

我不明白如何解决这个问题,我已经讨论了这里的一些问题,但没有找到一个完美的答案

我有一个数据框,它有以下重要的列:building_id、area、height

我试图编写的UDF计算面积的平方根和高度之间的差值。它返回一个值,该值应添加到数据帧中

def calculate_difference(area, height):
  # calculate the square root of the area
  import numpy as np
  nr = np.sqrt(area)
  
  # calculate the difference between the square root of the area and the height
  dif = nr - height
  
  return dif
然后我注册了这个UDF:

calculate_differenceUDF = udf(calculate_difference)
当我传递两个数字时,函数工作,它返回我期望的值。我想在我的dataframe中添加一个新列,其中我们有一个基于函数的计算值

display(df.withColumn("diff", calculate_differenceUDF(col("area"), col("height"))))
然后我收到这个错误:

PickleException:构造ClassDict应为零参数 (用于numpy.dtype)


我知道我没有返回正确的类型,但我不知道如何修复它!:)

我认为应该首先将
numpy.sqrt()
的返回值转换为python的float类型

def calculate_difference(area, height):
  
  nr = float(np.sqrt(area))
  dif = nr - height
  return dif
然后注册UDF

calculate_differenceUDF = udf(calculate_difference, FloatType())