【问题标题】:PickleException: expected zero arguments for construction of ClassDict (for numpy.dtype)PickleException:构造 ClassDict 的预期零参数(用于 numpy.dtype)
【发布时间】:2021-07-31 14:47:03
【问题描述】:

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

我有一个数据框,其中包含以下重要列: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)

当我传递两个数字时,该函数会起作用,它会返回我期望的值。我想在我的数据框中添加一个新列,其中我们有一个基于函数的计算值。

display(df.withColumn("diff", calculate_differenceUDF(col("area"), col("height"))))

然后我收到此错误:

PickleException:构造 ClassDict 的预期参数为零 (对于 numpy.dtype)

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

【问题讨论】:

    标签: pyspark user-defined-functions databricks


    【解决方案1】:

    我觉得你应该先把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())
    

    【讨论】:

      猜你喜欢
      • 2015-07-06
      • 2019-05-16
      • 2020-07-25
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多