【问题标题】:TypeError: a float is required pysparkTypeError:需要浮点数
【发布时间】:2018-07-31 09:33:32
【问题描述】:

我的目标是计算 lat-long 数据的半正弦距离,我已将 uci 数据集用于相同的 https://archive.ics.uci.edu/ml/datasets/GPS+Trajectories (go_track_trackspoints.csv) 我用下面的代码来计算距离

def dist(lon2, lat2,lon1, lat1):
            phi_1=toRadians(lat1)
            phi_2=toRadians(lat2)
            delta_phi=toRadians(lat2-lat1)
            delta_lambda=toRadians(lon2-lon1)


            a=sin(delta_phi/2.0)**2+cos(phi_1)*cos(phi_2)*sin(delta_lambda/2.0)**2
            c=2*atan2(sqrt(abs(a)),sqrt(abs((1-a))))
            return c * 6372.8

而架构是

root
 |-- id: string (nullable = true)
 |-- latitude: string (nullable = true)
 |-- longitude: string (nullable = true)
 |-- track_id: string (nullable = true)
 |-- time: string (nullable = true)

我已将数据加载到 spark 数据帧 (gps_data)

 +---+-----------------+-----------------+--------+-------------------+
    | id|         latitude|        longitude|track_id|               time|
    +---+-----------------+-----------------+--------+-------------------+
    |  1|-10.9393413858164|-37.0627421097422|       1|2014-09-13 07:24:32|
    |  2| -10.939341385769|-37.0627421097809|       1|2014-09-13 07:24:37|
    |  3|-10.9393239478718|-37.0627645137212|       1|2014-09-13 07:24:42|
    |  4|-10.9392105616561|-37.0628430455445|       1|2014-09-13 07:24:47|
    +---+-----------------+-----------------+--------+-------------------+

使用下面的命令我想得到一列距离

my_window = Window.partitionBy().orderBy("time")  
gps_d=gps_data.withColumn("dist", dist(
        "longitude", "latitude",
        lag("longitude", 1).over(my_window), lag("latitude", 1).over(my_window)
    ).alias("dist"))

但我无法解决错误,也无法找到解决方案。请帮助我!

错误是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-219-6352041ff223> in <module>()
      1 gps_d=gps_data.withColumn("dist", dist(
      2     "longitude", "latitude",
----> 3     lag("longitude", 1).over(my_window), lag("latitude", 1).over(my_window)
      4 ).alias("dist"))

<ipython-input-218-2f781ab3b2fb> in dist(lon2, lat2, lon1, lat1)
      9 
     10         a=sin(delta_phi/2.0)**2+cos(phi_1)*cos(phi_2)*sin(delta_lambda/2.0)**2
---> 11         c=2*atan2(sqrt(a),sqrt(1-a))
     12         return c * 6372.8

TypeError: a float is required

PS:我已经检查过在将 csv 加载到数据框时没有一列是空的

【问题讨论】:

  • 检查sqrt 的两个参数是否为正。没有看到代码就无法确定,但您可能将一个复数传递给atan2
  • @TomZych 在使用 abs 但同样的错误后尝试。请看上面我贴的代码。
  • @RameshMaharjan 使用了这个 from pyspark.sql.functions import atan2, cos, sin, lit, toRadians
  • 哦,好吧 :) 它没有向我显示错误。它给了我 null 所以我传递了列而不是字符串并且它起作用了
  • @RameshMaharjan 请告诉我你做了什么?

标签: python python-3.x apache-spark pyspark


【解决方案1】:

我的错误没有被复制,但输出也全部为空。所以我传递了列而不是字符串,它的工作原理是

gps_data.withColumn("dist", dist(
    col("longitude"), col("latitude"),
    lag("longitude", 1).over(my_window), lag("latitude", 1).over(my_window)
).alias("dist")).show(truncate=False)

输出为

+---+-----------------+-----------------+--------+-------------------+---------------------+
|id |latitude         |longitude        |track_id|time               |dist                 |
+---+-----------------+-----------------+--------+-------------------+---------------------+
|1  |-10.9393413858164|-37.0627421097422|1       |2014-09-13 07:24:32|null                 |
|2  |-10.939341385769 |-37.0627421097809|1       |2014-09-13 07:24:37|6.756720378438061E-9 |
|3  |-10.9393239478718|-37.0627645137212|1       |2014-09-13 07:24:42|0.0031221549946508337|
|4  |-10.9392105616561|-37.0628430455445|1       |2014-09-13 07:24:47|0.01525123103019258  |
+---+-----------------+-----------------+--------+-------------------+---------------------+

希望对你有帮助

【讨论】:

  • 还是报错,你用的是哪个spark版本?
  • spark 2.3 和你的?
  • 我使用的是 spark 2.0.2
  • 我刚刚在 2.0.2 中测试过,它的工作正常与 2.3.0 中一样。所以我想你必须看看错误是在哪里产生的
  • 哦...我将再次运行整个脚本并确认它是否有效。
猜你喜欢
  • 1970-01-01
  • 2019-10-16
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
相关资源
最近更新 更多